Service是Android中一個類,它是Android 四大組件之一,使用Service可以在后臺執行耗時的操作(注意需另啟子執行緒),其中Service并不與用戶產生UI互動,其他的應用組件可以啟動Service,即便用戶切換了其他應用,啟動的Service仍可在后臺運行,一個組件可以與Service系結并與之互動,甚至是跨行程通信,通常情況下Service可以在后臺執行網路請求、播放音樂、執行檔案讀寫操作或者與contentprovider互動等,
本文主要講述service服務里的啟動與系結服務,
首先,XML程式如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="operate"
android:text="啟動服務" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服務"
android:onClick="operate" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="系結服務"
android:onClick="operate" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解綁服務"
android:onClick="operate"/>
</LinearLayout>
創建一個service類,并寫創建、啟動、系結、摧毀、解綁五個方法
代碼如下:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private int i;
public MyService() {
}
//創建
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG","服務創建了");
}
class MyBinder extends Binder {
//定義自己需要的方法(實作進度監控)
public int getProcess(){
return i;
}
}
//啟動方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","服務啟動了");
return super.onStartCommand(intent, flags, startId);
}
//解綁
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG","服務解綁了");
return super.onUnbind(intent);
}
//摧毀
@Override
public void onDestroy() {
Log.e("TAG","服務摧毀了");
super.onDestroy();
}
//系結方法
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
Log.e("TAG","服務系結了");
return new MyBinder();
}
}
然后在MainActivity里面寫點擊啟動方法:
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//啟動服務 :創建——啟動——摧毀
//如果服務已經創建了,后續重復啟動,操作的都是同一個服務,不回在創建新 的服務,除非先摧毀他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
}
}
然后寫系結的方法:
注意:捆綁和解綁的物件應該是同一個物件,如果捆綁與解綁的物件不一樣,則會報如下的錯誤:
Service not registered: com.m1910.servicetest.MainActivity$1@41ddfcc0
本篇文章捆綁與解綁的物件都是conn,定義一個全域變數:
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
然后寫解綁與捆綁的方法:
case R.id.bind:
//系結服務
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解綁服務
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解綁服務
// isBound = false;
// }
unbindService(conn);//解綁服務
break;
完整的程式如下:
package com.example.service;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private boolean isBound = false;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//啟動服務 :創建——啟動——摧毀
//如果服務已經創建了,后續重復啟動,操作的都是同一個服務,不回在創建新 的服務,除非先摧毀他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
case R.id.bind:
//系結服務
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解綁服務
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解綁服務
// isBound = false;
// }
unbindService(conn);//解綁服務
break;
}
}
}
Service基礎(實作IBinder)(二)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/398032.html
標籤:AI
