Android service的開啟和綁定,以及調用service的方法

來源:互聯網
上載者:User

標籤:des   android   style   blog   class   code   

介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >    <Button android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="開啟服務"            android:onClick="start"            />    <Button android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="關閉服務"            android:onClick="close"            />    <Button android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="綁定服務"            android:onClick="bind"            />    <Button android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="取消綁定服務"            android:onClick="unbind"            />    <Button android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="調用服務的方法function()"            android:onClick="callFunction"            /></LinearLayout>

 

Activity

package com.example.serviceTest;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;public class MyActivity extends Activity {    private IService myBinder;    private ServiceConnection myConn = new MyConn();    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    public void start(View view) {        startService(new Intent(this, TestService.class));    }    /**     * 多次調用停止服務,沒有出現問題(不能多次解除綁定)     */    public void close(View view) {        stopService(new Intent(this, TestService.class));    }    public void bind(View view) {        Intent intent = new Intent(this, TestService.class);        bindService(intent, myConn, BIND_AUTO_CREATE);    }    /**     * 多次解除綁定服務會報出錯誤     */    public void unbind(View view) {        unbindService(myConn);    }    public void callFunction(View view) {        if (myBinder != null) {            myBinder.callFunction();        }    }    //綁定的時候,回調的一些方法    class MyConn implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            System.out.println("擷取到binder");            myBinder = (IService) service;        }        @Override        public void onServiceDisconnected(ComponentName name) {            System.out.println("com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected");        }    }}


Service

package com.example.serviceTest;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;/** * Created by Heyiyong on 14-5-16. */public class TestService extends Service {    public IBinder onBind(Intent intent) {        System.out.println("服務被綁定了!");        return new MyBinder();    }    //中間人(service的代理)    private class MyBinder extends Binder implements IService{        public void callFunction() {            function();        }    }    @Override    public void onCreate() {        System.out.println("com.example.serviceTest.TestService.onCreate");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        System.out.println("com.example.serviceTest.TestService.onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        System.out.println("com.example.serviceTest.TestService.onDestroy");    }    @Override    public boolean onUnbind(Intent intent) {        System.out.println("com.example.serviceTest.TestService.onUnbind");        return super.onUnbind(intent);    }    public void function() {        Toast.makeText(getApplicationContext(), "function()方法被調用了!", 1).show();        System.out.println("com.example.serviceTest.TestService.function");    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.