Android-----LocalService

LocalService乃只能由承载该Service的Application访问,无法提供在设备上运行的其他Application访问。

LocalService由Context.startCommand()启动,启动以后,这些类型的服务将持续运行,知道客户端调用服务的Context.stopService()或者服务自己调用stopSelf()。如果在Service启动之后调用Context.startCommand()不会为Service创建另一个实例,但这样做将重新调用正在运行的Service的onStartCommand()方法。

下面将用一个实例进行解析
 
BackgroundService

/**
 * 创建后台服务
 * @author Administrator
 *
 */
public class BackgroundService extends Service {

    private static final String TAG = "BackgroundService";
    //通知栏
    private NotificationManager notificationManager;
    //线程池
    private ThreadGroup threadGroup = new ThreadGroup("ServiceWorker");
    
    @Override
    public void onCreate(){
        super.onCreate();
        Log.e(TAG, "onCreate()");
        
        notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
        displayNoticationMessage("Background Service is running");
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        super.onStartCommand(intent, flags, startId);
    
        int counter = intent.getExtras().getInt("counter");
        Log.e(TAG, "in onStartCommand(),counter = " + counter +",start = " + startId);
        //创建并启用服务工作者监听线程
        new Thread(threadGroup , new ServiceWorker(counter) , "BackgroundService").start();
        return START_STICKY;
    }
    
    //创建服务工作者线程动作
    class ServiceWorker implements Runnable{

        private int counter = -1;
        public ServiceWorker(int counter){
            this.counter = counter;
        }
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
            try {
                Log.e(TAG2, "sleeping for 10 seconds.counter = " + counter);
                Thread.sleep(10000);
                Log.e(TAG2, "...waking up");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    @Override
    public void onDestroy(){
        Log.e("", "in onDestroy()");
        threadGroup.interrupt();
        notificationManager.cancelAll();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.e(TAG, "in onBind");
        return null;
    }
    
    @SuppressWarnings("deprecation")
    private void displayNoticationMessage(String message) {
        // TODO Auto-generated method stub
        Notification notification = new Notification(R.drawable.emo_im_winking,message,System.currentTimeMillis());
        notification.flags = Notification.FLAG_NO_CLEAR;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
        notification.setLatestEventInfo(this, TAG, message, pendingIntent);
        notificationManager.notify(0, notification);
    }
}

 

 

调用服务

Intent intent = new Intent(this,BackgroundService.class);
    intent.putExtra("counter", counter);
    startService(intent);

 

停止服务

if(stopService(new Intent(this,BackgroundService.class)))
            Log.e(TAG, "StopService is successful");
   else
            Log.e(TAG, "StopService is unsuccessful");
posted @ 2014-01-28 21:29  VIJAY-YAN  阅读(465)  评论(0编辑  收藏  举报