android Activity綁定Service

來源:互聯網
上載者:User

標籤:

activity可以綁定Service,並且可以調用Service中定義的方法

 

Service代碼:在裡面多了一個IBinder;個人理解是用來與Activity綁定的主要通道;

public class MainServer extends Service {    private final String TAG = "Service------->";    private final IBinder binder = new MyBinder();        //綁定器    public class MyBinder extends Binder {        public MainServer getService() {            return MainServer.this;    //返回本服務            }    }    /** 綁定時執行 */    @Override    public IBinder onBind(Intent intent) {        Log.i(TAG, "onBind()");        return binder;    }    /** 只在建立時執行一次 */    @Override    public void onCreate() {        super.onCreate();        Log.i(TAG, "onCreate()");    }    /** 斷開綁定或者停止服務時執行 */    @Override    public void onDestroy() {        super.onDestroy();        Log.i(TAG, "onDestroy()");    }    /** 當記憶體不夠是執行該方法 */    @Override    public void onLowMemory() {        super.onLowMemory();        Log.i(TAG, "onLowMemory()");        onDestroy();    }    /** 當從新綁定是執行 */    @Override    public void onRebind(Intent intent) {        // TODO Auto-generated method stub        super.onRebind(intent);        Log.i(TAG, "onRebind()");    }    @Override    public void onStart(Intent intent, int startId) {        // TODO Auto-generated method stub        super.onStart(intent, startId);        Log.i(TAG, "onStart()");    }    /** 每次執行Service都會執行該方法 */    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "onStartCommand --->flags = " + flags + "     startId = " + startId);        con = new Connect();        con.start();        return super.onStartCommand(intent, flags, startId);    }    /** 斷開綁定是執行 */    @Override    public boolean onUnbind(Intent intent) {        Log.i(TAG, "onUnbind()");        return super.onUnbind(intent);    }            


 

MainActivity代碼:  必須聲明執行個體ServiceConnection,並且在綁定成功後擷取到Service,用以調用Service的方法

public class MainActivity extends Activity {        private MainServer mainService;    //綁定Service監聽    ServiceConnection sconnection = new ServiceConnection() {        @Override        public void onServiceDisconnected(ComponentName name) {            Log.i("activity---->", "已斷開Service");        }        /**當綁定時執行*/        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            // TODO Auto-generated method stub            Log.i("activity---->", "已綁定到Service");            mainService = ((MainServer.MyBinder)service).getService();            Intent i = new Intent();            mainService.onStartCommand(i, 0, 0);        //綁定成功後可以調用Service方法,此處等與是啟動Service        }    };        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button start = (Button)findViewById(R.id.start);        Button stop = (Button)findViewById(R.id.stop);        Button send = (Button)findViewById(R.id.send);        Button skip = (Button)findViewById(R.id.skip);                start.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent bind = new Intent(MainActivity.this,MainServer.class);                bindService(bind, sconnection, Context.BIND_AUTO_CREATE);                            }        });        stop.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                unbindService(sconnection);            }        });

 

android Activity綁定Service

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.