初识 Service(二) 演示: Activity 调用Service的接口
2011-11-11 17:05
639 查看
先看代码:
Service的代码:
另外,需注意:
1 如果onBinder返回的是null, 则不会调用onServiceConnected()
2 unbindService()却不会调用onServiceDisconnected接口. unbindService只是告诉系统,你的APP已经跟这个service没什么关系了,
系统有权回收这个Service。
工程代码下载: http://download.csdn.net/detail/gdp2852/3783686
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(三) 演示:Service给 Activity传递消息
- Android如何在Activity中调用Screen off 和 Screen on接口
- 在Activity中调用本地服务(Service)中的方法
- system_service进程里 调用SystemManager.getService("activity") 直接返回ams的引用?
- Activity与Service绑定(activity调用service方法)--安卓软件开发
- 安卓跨应用调用Activity,Service并进行通信,AIDL的应用
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- [置顶] Java WebService接口生成和调用 图文详解
- Java WebService接口生成和调用 图文详解
- WF工作流技术内幕 —— 以InvokeWebServiceActivity在Workflow工作流调用Web服务
- Java WebService接口生成和调用
- Activity调用Service的方法
- 在Activity中调用本地服务(Service)中的方法
- Android 在Service中调用Activity
- WF工作流技术内幕 —— 以InvokeWebServiceActivity在Workflow工作流调用Web服务
- Android中Recyclerview使用7----条目中按钮点击事件,在activity中调用(接口回调)
- controller调用service接口 空指针异常
- 在springmvc中非controller类中调用service接口,以获取springmvc容器方法实现
- httpPost/httpGet接口调用werService接口,客户端写法