Service绑定Activity

来源:互联网 发布:jquery.base64.js 编辑:程序博客网 时间:2024/05/17 01:23

<1>BindService.java

package com.example.bindservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class BindService extends Service {private int count;private boolean quit;private MyBinder binder = new MyBinder();public class MyBinder extends Binder{public int getCount(){return count;}}@Overridepublic IBinder onBind(Intent intent) {System.out.println("Service is binded!");return binder;}@Overridepublic void onCreate() {super.onCreate();System.out.println("Service is created!");new Thread(){@Overridepublic void run() {while(!quit){try {Thread.sleep(1000);} catch (InterruptedException e) {}count++;}}}.start();}@Overridepublic void onDestroy() {super.onDestroy();this.quit = true;System.out.println("Service is destroyed!");}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("Service is unbinded!");return true;}}
<2>MainActivity.java

package com.example.bindservice;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button btnBind, btnUnbind, btnGetServiceStatus;BindService.MyBinder binder;private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {System.out.println("Service disconnected!");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {System.out.println("Service connected!");binder = (BindService.MyBinder)service;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnBind = (Button)findViewById(R.id.btn1);btnUnbind = (Button)findViewById(R.id.btn2);btnGetServiceStatus = (Button)findViewById(R.id.btn3);final Intent intent = new Intent();intent.setAction("org.crazyit.service.BIND_SERVICE");btnBind.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {bindService(intent, conn, Service.BIND_AUTO_CREATE);}});}}
<3>AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.bindservice"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.bindservice.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <service android:name=".BindService">            <intent-filter>                <action android:name="org.crazyit.service.BIND_SERVICE"/>            </intent-filter>        </service>            </application></manifest>


0 0