zoukankan      html  css  js  c++  java
  • 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");
  • 相关阅读:
    C# winform 获取标题栏,状态栏,菜单栏的高度
    <转载>OleDb操作Access数据库:新增记录时获取自动编号的主键值
    《名利场》:微软 “ 失落的十年”
    Lisp的永恒之道(转)
    Google Earth KML数据格式转换成Shp数据格式(转)
    【转】ArcGIS投影转换与坐标转换
    利用Mono.Cecil动态修改程序集来破解商业组件(仅用于研究学习)
    C# mouseDoubleClick与DoubleClick的关系
    ACCESS通用操作数据类
    VS2010单元测试入门实践教程
  • 原文地址:https://www.cnblogs.com/vijay/p/3535927.html
Copyright © 2011-2022 走看看