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
- 绑定Activity和Service
- Activity和Service绑定
- Activity 和Service绑定
- Service 和 Activity 绑定
- Activity 和Service绑定
- Activity 和Service绑定
- 绑定Activity和Service
- Activity绑定service
- Activity 和Service绑定
- Activity 和Service绑定
- Service绑定Activity
- Activity绑定Service
- Tabhost中Activity绑定Service
- service和activity绑定步骤
- Tabhost中Activity绑定Service
- android service 与activity通信--绑定service
- Service基础Activity与Service绑定
- Android:Service的绑定和解绑定,Service与Activity通信
- 倒三角形
- Java中String类的isEmpty方法、null以及""的区别
- 用GUID做主键与int做主键的区别(转)
- ZooKeeper的安装
- 去除ScrollView滑动到尽头时再拖动的阴影效果
- Service绑定Activity
- Lua table用法
- android程序右上角ActionBar上显示三个点(overflow)
- 如何入门 Python 爬虫?
- Dolby Digital(Dolby Surround Digital,杜比数码环绕声)
- 系统时间自动同步服务导致C#定时器失效的问题
- 如何正确获取Android控件的高度
- MySQL数据类型和常用字段属性总结
- Centos 7 安装 Google chrome 浏览器