android?Service基礎(chǔ)(啟動服務(wù)與綁定服務(wù))
Service是Android中一個類,它是Android 四大組件之一,使用Service可以在后臺執(zhí)行耗時的操作(注意需另啟子線程),其中Service并不與用戶產(chǎn)生UI交互。其他的應(yīng)用組件可以啟動Service,即便用戶切換了其他應(yīng)用,啟動的Service仍可在后臺運行。一個組件可以與Service綁定并與之交互,甚至是跨進程通信。通常情況下Service可以在后臺執(zhí)行網(wǎng)絡(luò)請求、播放音樂、執(zhí)行文件讀寫操作或者與contentprovider交互等。
本文主要講述service服務(wù)里的啟動與綁定服務(wù)。
首先,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="啟動服務(wù)" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服務(wù)"
android:onClick="operate" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="綁定服務(wù)"
android:onClick="operate" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解綁服務(wù)"
android:onClick="operate"/>
</LinearLayout>創(chuàng)建一個service類,并寫創(chuàng)建、啟動、綁定、摧毀、解綁五個方法
代碼如下:
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() {
}
//創(chuàng)建
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG","服務(wù)創(chuàng)建了");
}
class MyBinder extends Binder {
//定義自己需要的方法(實現(xiàn)進度監(jiān)控)
public int getProcess(){
return i;
}
}
//啟動方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG","服務(wù)啟動了");
return super.onStartCommand(intent, flags, startId);
}
//解綁
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG","服務(wù)解綁了");
return super.onUnbind(intent);
}
//摧毀
@Override
public void onDestroy() {
Log.e("TAG","服務(wù)摧毀了");
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","服務(wù)綁定了");
return new MyBinder();
}
}然后在MainActivity里面寫點擊啟動方法:
public void operate(View v) {
switch (v.getId()){
case R.id.start:
//啟動服務(wù) :創(chuàng)建——啟動——摧毀
//如果服務(wù)已經(jīng)創(chuàng)建了,后續(xù)重復(fù)啟動,操作的都是同一個服務(wù),不回在創(chuàng)建新 的服務(wù),除非先摧毀他
Intent it1=new Intent(this,MyService.class);
startService(it1);
break;
case R.id.stop:
Intent it2=new Intent(this,MyService.class);
stopService(it2);
break;
}
}然后寫綁定的方法:
注意:捆綁和解綁的對象應(yīng)該是同一個對象,如果捆綁與解綁的對象不一樣,則會報如下的錯誤:
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:
//綁定服務(wù)
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解綁服務(wù)
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解綁服務(wù)
// isBound = false;
// }
unbindService(conn);//解綁服務(wù)
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:
//啟動服務(wù) :創(chuàng)建——啟動——摧毀
//如果服務(wù)已經(jīng)創(chuàng)建了,后續(xù)重復(fù)啟動,操作的都是同一個服務(wù),不回在創(chuàng)建新 的服務(wù),除非先摧毀他
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:
//綁定服務(wù)
Intent it3=new Intent(this,MyService.class);
bindService(it3,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//解綁服務(wù)
// unbindService(conn);
// if (isBound) {
// unbindService(conn);// 解綁服務(wù)
// isBound = false;
// }
unbindService(conn);//解綁服務(wù)
break;
}
}
}到此這篇關(guān)于android Service基礎(chǔ)(啟動服務(wù)與綁定服務(wù))的文章就介紹到這了,更多相關(guān)android Service內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Flutter實現(xiàn)3D動畫效果示例詳解
在Flutter中提供了AnimatedWidget組件用于構(gòu)建可復(fù)用的動畫組件。本文我們用AnimatedWidget來實現(xiàn)組件的3D旋轉(zhuǎn)效果,感興趣的可以了解一下2022-03-03
Kotlin標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法基礎(chǔ)知識詳解
Kotlin中的標(biāo)準(zhǔn)函數(shù)指的是Standard.kt文件中定義的函數(shù),任何Kotlin代碼都可以自由地調(diào)用所有的標(biāo)準(zhǔn)函數(shù)。例如let這個標(biāo)準(zhǔn)函數(shù),他的主要作用就是配合?.操作符來進行輔助判空處理2022-11-11
Android實現(xiàn)listview動態(tài)加載數(shù)據(jù)分頁的兩種方法
這篇文章主要為大家詳細介紹了Android實現(xiàn)listview動態(tài)加載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06

