1.概述
Service(服务)是一个一种可以在后台执行长时间运行操作而没有用户界面的应用组件。服务可由其他应用组件启动(如Activity),服务一旦被启动将在后台一直运行,即使启动服务的组件(Activity)已销毁也不受影响。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。
代码如下:
Myservice.java:
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
//此java文件为自定义的service
public class MyService extends Service {
private int count;
private boolean quit;
private MyBinder binder = new MyBinder();
public MyService() {
}
public class MyBinder extends Binder{
public int getCount(){
return count;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.e("Service","Service Binding~");
return binder ;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("Service","Service Creating~");
new Thread(){
@Override
public void run() {
super.run();
while(!quit){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("Service","Service Starting~");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("Service","Service Destroying~");
quit = true;
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("Service","Service unBinding~");
return super.onUnbind(intent);
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ServiceActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="Service"
android:textColor="@color/colorPrimary"
android:textSize="70dp"
android:id="@+id/tv"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Start"
android:id="@+id/btn1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv"
app:layout_constraintLeft_toRightOf="@id/btn1"
android:text="Stop"
android:id="@+id/btn2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Bind"
android:id="@+id/btn3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:layout_constraintLeft_toRightOf="@id/btn3"
android:text="Get"
android:id="@+id/btn4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:layout_constraintLeft_toRightOf="@id/btn4"
android:text="unBind"
android:id="@+id/btn5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn3"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Play"
android:id="@+id/btn6"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity.java :
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ServiceActivity extends AppCompatActivity {
public Button btn1;
public Button btn2;
public Button btn3;
public Button btn4;
public Button btn5;
public Button btn6;
public TextView tv;
public MyService.MyBinder binder;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("Service","Service Connected!");
binder= (MyService.MyBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("Service","Service DisConnected!");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
tv = findViewById(R.id.tv);//绑定按钮
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
final Intent intent = new Intent();
intent.setAction("com.example.myapplication.MyService");
intent.setPackage("com.example.myapplication");
//下面为button的响应函数
//开始服务
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(intent);
}
});
//停止服务
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
}
});
//绑定服务
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent,conn,BIND_AUTO_CREATE);
}
});
//得到count数
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(String.valueOf(binder.getCount()));//获取到数据,展示到TextView上去
}
});
//解除绑定
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);
}
});
//play music
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
MediaPlayer mediaPlayer = MediaPlayer.create(ServiceActivity.this,R.raw.chengzao );
mediaPlayer.start();
}
});
}
}