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();
}
});
}
}