Service的用法

一、Service 

  一个Service必须继承Service类,实现onBind()方法,返回IBinder 给客户端

二、客户端

   采用bindService(intent, mConnection,Context.BIND_AUTO_CREATE);。当建立连接后,会给我们一个对象mconnetion,

 

ServiceConnection mConnection是和service进行通信的主要接口,我们可以使用它和service进行通信。

 

注意点:由于手机内存不足时,service可能被收回,可以使用startForeground()保证Service不被收回;

Notification notification =newNotification(R.drawable.icon, getText(R.string.ticker_text),
       
System.currentTimeMillis());
Intent notificationIntent =newIntent(this,ExampleActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(this,0, notificationIntent,0);
notification
.setLatestEventInfo(this, getText(R.string.notification_title),
        getText
(R.string.notification_message), pendingIntent);
startForeground
(ONGOING_NOTIFICATION_ID, notification);

posted on 2014-04-08 14:59  axiaoquana  阅读(118)  评论(0)    收藏  举报

导航