?? 為什么我用 unbindService 方法解除了綁定后,仍然可以在 活動中調(diào)用 服務(wù)中的方法,解除綁定后,服務(wù)確實已經(jīng)調(diào)用了 onDestory方法銷毀了服務(wù),為什么還能調(diào)用服務(wù)中的方法呢??? ?? 通常情況下 bindService()這個方法是怎么用的呢?活動的代碼:package?com.example.myservicetest;
?
import?com.example.myservicetest.MyService.*;
?
import?android.app.*;
import?android.content.*;
import?android.view.*;
import?android.view.View.*;
import?android.os.*;
import?android.util.Log;
import?android.widget.*;
?
public?class?MainActivity?extends?Activity?implements?OnClickListener?{
?
??//--------------------------------------------------------------------
??private?Button?b1,?b2,?b3,?b4,?b5;
??private?ServiceConnection?connection;
??private?MyService?myService;
??private?Intent?in;
???
??//--------------------------------------------------------------------
??@Override
??protected?void?onCreate(Bundle?savedInstanceState)
??{
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
?????
????//獲取按鈕
????b1?=?(Button)findViewById(R.id.bind);
????b2?=?(Button)findViewById(R.id.unbind);
????b3?=?(Button)findViewById(R.id.play);
????b4?=?(Button)findViewById(R.id.start);
????b5?=?(Button)findViewById(R.id.stop);
?????
????//注冊點(diǎn)擊事件
????b1.setOnClickListener(this);
????b2.setOnClickListener(this);
????b3.setOnClickListener(this);
????b4.setOnClickListener(this);
????b5.setOnClickListener(this);
?????
????//用于連接服務(wù)
????connection?=?new?ServiceConnection()
????{
??????@Override
??????public?void?onServiceDisconnected(ComponentName?name)
??????{
????????Log.e("connection",?"連接意外丟失");
??????}
???????
??????@Override
??????public?void?onServiceConnected(ComponentName?name,?IBinder?service)
??????{
????????myService?=?((MyBinder)service).getMyService();
????????Log.e("connection",?"連接完成");
??????}
????};
?????
????//啟動服務(wù)意圖
????in?=?new?Intent(this,?MyService.class);
?????
??}
?
??//--------------------------------------------------------------------
??@Override
??public?void?onClick(View?v)
??{
????switch(v.getId())
????{
????//啟動服務(wù)
????case?R.id.start:
??????startService(in);
??????break;
???????
????//停止服務(wù)
????case?R.id.stop:
??????stopService(in);
??????break;
???????
????//綁定服務(wù)
????case?R.id.bind:
??????bindService(new?Intent(this,?MyService.class),?connection,?BIND_AUTO_CREATE);
??????break;
???????
????//解綁服務(wù)
????case?R.id.unbind:
??????unbindService(connection);
??????break;
???????
????//調(diào)用服務(wù)中的方法
????case?R.id.play:
??????myService.play();
??????break;
????}
??}
?
}服務(wù)的代碼:package?com.example.myservicetest;
?
import?android.app.*;
import?android.content.*;
import?android.os.*;
import?android.util.Log;
?
public?class?MyService?extends?Service?{
?
??private?MyBinder?mBinder;
?
??public?class?MyBinder?extends?Binder?{
????public?MyService?getMyService()
????{
??????return?MyService.this;
????}
??}
?
??//--------------------------------------------------------------------
??@Override
??public?void?onCreate()
??{
????super.onCreate();
????mBinder?=?new?MyBinder();
????Log.e("Service",?"創(chuàng)建服務(wù)");
??}
?
??//--------------------------------------------------------------------
??@Override
??public?IBinder?onBind(Intent?intent)
??{
????Log.e("Service",?"執(zhí)行onBind");
????return?mBinder;
??}
?
??//--------------------------------------------------------------------
??@Override
??public?int?onStartCommand(Intent?intent,?int?flags,?int?startId)
??{
????Log.e("Service",?"執(zhí)行onStartCommand");
????return?START_NOT_STICKY;
??}
???
??//--------------------------------------------------------------------
??@Override
??public?boolean?onUnbind(Intent?intent)
??{
????Log.e("Service",?"執(zhí)行onUnbind");
????return?false;
??}
???
??//--------------------------------------------------------------------
??@Override
??public?void?onDestroy()
??{
????Log.e("Service",?"服務(wù)銷毀");
????super.onDestroy();
??}
???
???
???
???
??//--------------------------------------------------------------------
??public?void?play()
??{
????Log.e("binder",?"播放音樂");
??}
?
}
- 0 回答
- 0 關(guān)注
- 2844 瀏覽
添加回答
舉報
0/150
提交
取消