3 回答
TA貢獻1830條經驗 獲得超9個贊
您可以嘗試使用以下代碼:
protected ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(LOG_TAG, "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(LOG_TAG, "onServiceDisconnected");
}
}
public void start() {
// mContext is defined upper in code, I think it is not necessary to explain what is it
mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(intent);
}
public void stop() {
mContext.stopService(new Intent(mContext, ServiceRemote.class));
mContext.unbindService(mServerConn);
}
TA貢獻1789條經驗 獲得超10個贊
將以下方法添加到您的活動中:
private MyService myServiceBinder;
public ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
myServiceBinder = ((MyService.MyBinder) binder).getService();
Log.d("ServiceConnection","connected");
showServiceData();
}
public void onServiceDisconnected(ComponentName className) {
Log.d("ServiceConnection","disconnected");
myService = null;
}
};
public Handler myHandler = new Handler() {
public void handleMessage(Message message) {
Bundle data = message.getData();
}
};
public void doBindService() {
Intent intent = null;
intent = new Intent(this, BTService.class);
// Create a new Messenger for the communication back
// From the Service to the Activity
Messenger messenger = new Messenger(myHandler);
intent.putExtra("MESSENGER", messenger);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}
您可以通過在Activity類上ovverriding onResume()和onPause()綁定到服務。
@Override
protected void onResume() {
Log.d("activity", "onResume");
if (myService == null) {
doBindService();
}
super.onResume();
}
@Override
protected void onPause() {
//FIXME put back
Log.d("activity", "onPause");
if (myService != null) {
unbindService(myConnection);
myService = null;
}
super.onPause();
}
請注意,綁定到服務時,僅onCreate()在服務類中調用該方法。在您的Service類中,您需要定義myBinder方法:
private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;
@Override
public IBinder onBind(Intent arg0) {
Bundle extras = arg0.getExtras();
Log.d("service","onBind");
// Get messager from the Activity
if (extras != null) {
Log.d("service","onBind with extra");
outMessenger = (Messenger) extras.get("MESSENGER");
}
return mBinder;
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
定義了這些方法之后,您可以在“活動”中找到服務的方法:
private void showServiceData() {
myServiceBinder.myMethod();
}
最后,當某些事件發生時,例如_BOOT_COMPLETED_,您可以啟動服務
public class MyReciever extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.intent.action.BOOT_COMPLETED")) {
Intent service = new Intent(context, myService.class);
context.startService(service);
}
}
}
需要注意的是啟動服務的時候onCreate()和onStartCommand()被調用服務類,當另一個事件發生時由您可以停止你的服務stopService() 請注意,事件偵聽器應該是在你的Android清單文件registerd:
<receiver android:name="MyReciever" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
TA貢獻1811條經驗 獲得超5個贊
首先,我們需要了解兩件事,
客戶
它向特定服務器發出請求
bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
這mServiceConn是ServiceConnection類(內置)的實例,它實際上是我們需要用兩種方法(用于連接網絡的第一個和未連接網絡的第二個)實現的接口,以監視網絡連接狀態。
服務器
它處理客戶端的請求,并制作自己的副本,該副本僅對發送請求的客戶端是私有的,并且此服務器在不同線程上運行。
現在在客戶端,如何訪問服務器的所有方法?
服務器發送帶有IBinder對象的響應。因此,IBinderobject是我們的處理程序,該處理程序Service使用(。)運算符訪問所有方法。
。
MyService myService;
public ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.d("ServiceConnection","connected");
myService = binder;
}
//binder comes from server to communicate with method's of
public void onServiceDisconnected(ComponentName className) {
Log.d("ServiceConnection","disconnected");
myService = null;
}
}
現在如何調用服務中的方法
myservice.serviceMethod();
這myService是對象,serviceMethod是服務中的方法。并以此方式在客戶端和服務器之間建立通信。
- 3 回答
- 0 關注
- 619 瀏覽
添加回答
舉報
