初识 Service(二) 演示: Activity 调用Service的接口
来源:互联网 发布:淘宝开业牌匾 编辑:程序博客网 时间:2024/05/01 13:01
先看代码:
import com.gdp2852.demo.service.BackgroundService.MyServiceBinder;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.util.Log;import android.view.View;public class Demo_ServiceActivity extends Activity {public final static String Tag = "Demo_Service"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.bindBtn).setOnClickListener(new View.OnClickListener() {public void onClick(View v) {bindService(new Intent(Demo_ServiceActivity.this,BackgroundService.class),srvConn,BIND_AUTO_CREATE);}}); findViewById(R.id.unbindBtn).setOnClickListener(new View.OnClickListener() {public void onClick(View v) {unbindService(srvConn);}}); } ServiceConnection srvConn = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) {Log.d(Tag, "onServiceConnected "+name);MyServiceBinder binder = (MyServiceBinder) service;binder.doLog(" Demo_ServiceActivity_CONTENT");} public void onServiceDisconnected(ComponentName name) { //在unbind和stop service都没有调用此函数Log.d(Tag, "onServiceDisconnected:"+name);}};}
Service的代码:
import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class BackgroundService extends Service {private MyServiceBinder binder = new MyServiceBinder();private NotificationManager notificationMrg;public void onCreate() {super.onCreate();notificationMrg = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);displayNotificationMessage("starting Background Service");doInBackground();}private void doInBackground() {new Thread(new Runnable() {public void run() {//TODO do what you wanna to do}}).start();}public void onDestroy() {displayNotificationMessage("stopping Background Service");super.onDestroy();}private void displayNotificationMessage(String message) {Notification notification = new Notification(R.drawable.sina, message,System.currentTimeMillis());PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, Demo_ServiceActivity.class), 0);notification.setLatestEventInfo(this, "Background Service", message,contentIntent);notificationMrg.notify(R.id.app_notification_id, notification);}public IBinder onBind(Intent intent) {return binder;}class MyServiceBinder extends Binder{void doLog(String logContent){Log.d(Demo_ServiceActivity.Tag, "invoke MyServiceBinder doLog:"+logContent);}}}
ServiceConnection srvConn = new ServiceConnection() { public void onServiceConnected在ServiceConnection连接的时会,可以从onServiceConnected()中拿到IBinder引用,这样就可以从通过IBinder调用Service的方法了
另外,需注意:
1 如果onBinder返回的是null, 则不会调用onServiceConnected()
2 unbindService()却不会调用onServiceDisconnected接口. unbindService只是告诉系统,你的APP已经跟这个service没什么关系了,
系统有权回收这个Service。
工程代码下载: http://download.csdn.net/detail/gdp2852/3783686
- 初识 Service(二) 演示: Activity 调用Service的接口
- 初识 Service(二) 演示: Activity 调用Service的接口
- 初识 Service(三) 演示:Service给 Activity传递消息
- Service初识(二)
- Activity调用Service的方法
- Android进阶(二) Activity调用Service 通过AIDL实现
- Android的Service与BroadcastReceiver之二 跨进程调用Service(AIDL Service)
- android Service(二) activity启动Service方式二:bindService()
- Activity调用Service里的方法
- Web Service 接口调用
- 调用web Service 接口
- Service的使用(二)之与activity的通讯
- Service与Activity之间的通讯(二)
- Activity与Service的交互二
- android的一些总结——service(二)之activity与service之间的通信
- 初识 Service(一)
- Service初识(一)
- service调用Activity中的方法
- 数塔 最优路径问题 -- (第九题)
- How to Find A Memory Leak
- 算法面试:精选微软经典的算法面试100题
- PB读取配置文件ini(ProfileString)
- 网络编程笔记<1>
- 初识 Service(二) 演示: Activity 调用Service的接口
- Linux内核通知链机制的原理及实现
- linux send recv函数详解
- volatile在debug和release模式下的意义
- 堆和栈的区别——转载自:百度
- 每对顶点间的最短路径C++实现
- phpExcel导出到Excel和pdf
- Java编写的五子棋游戏
- ORACLE OCi接口WIN下DEVC++工具Gcc编译器设置