码迷,mamicode.com
首页 > 其他好文 > 详细

粘性Service

时间:2015-09-19 21:02:38      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

粘性Service就是一种服务 把他删去他又会马上创建 原理是在这个服务中去开启线程不断检测此服务是否存在如果不存在,咋就会重新创建

技术分享
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 
 5 public class MainActivity extends Activity {
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         
12         startService(new Intent(getApplicationContext(),PushService.class));
13     }
14 
15     
16 
17 }
MainActivity.java
技术分享
 1 import java.util.List;
 2 
 3 import android.app.ActivityManager;
 4 import android.app.ActivityManager.RunningServiceInfo;
 5 import android.app.Service;
 6 import android.content.Intent;
 7 import android.os.IBinder;
 8 
 9 public class PushService extends Service {
10 
11     @Override
12     public void onCreate() {
13         // TODO Auto-generated method stub
14         super.onCreate();
15         
16         new SeekServiceThread().start();
17     }
18     
19     @Override
20     public int onStartCommand(Intent intent, int flags, int startId) {
21         return Service.START_STICKY;//绮樻?Service鏍囪瘑锛屽綋Service缁勪欢鍦ㄩ潪鎰忔効鏃惰鍋滄鍚庯紝鏈夋満鐜囬噸鍚?    
22         }
23     
24     @Override
25     public IBinder onBind(Intent intent) {
26         // TODO Auto-generated method stub
27         return null;
28     }
29     
30     
31     //鏌ユ壘褰撳墠缁勪欢鐨勭嚎绋嬶紝濡傛灉褰撳墠姝h繍琛岀殑鏈嶅姟缁勪欢涓嶅寘鍚玃ushService缁勪欢鏃讹紝鍒欏惎鍔?    
32     class SeekServiceThread extends Thread{
33         
34         @Override
35         public void run() {
36             
37             while(true){
38                 
39                 //1.鑾峰彇Activity缁勪欢绠$悊鍣紙绠$悊褰撳墠搴旂敤鐨勮繘绋嬨?鏈嶅姟缁勪欢銆佷换鍔℃垨鍥為?鏍堬級
40                 ActivityManager mgr=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
41                 
42                 //2. 鑾峰彇姝h繍琛岀殑鏈嶅姟缁勪欢
43                 List<RunningServiceInfo> rServices = mgr.getRunningServices(100);
44                 
45                 boolean isFinded=false;//鏍囪瘑鏄惁鏌ユ壘鍒板綋鍓嶇殑service缁勪欢
46                 for(RunningServiceInfo rService:rServices){
47                     if(rService.getClass().getName().equals(PushService.class.getName())){
48                         isFinded=true;
49                     }
50                 }
51                 
52                 if(!isFinded){ //濡傛灉娌℃湁鏌ユ壘鍒帮紝鍒欏惎鍔ㄦ湰鏈嶅姟缁勪欢
53                     
54                     startService(new Intent(getApplicationContext(),PushService.class));
55                 }
56                 
57                 try {
58                     Thread.sleep(2000);
59                 } catch (InterruptedException e) {
60                     e.printStackTrace();
61                 }
62             }
63             
64         }
65     }
66 
67 }
PushService.java

 

粘性Service

标签:

原文地址:http://www.cnblogs.com/bimingcong/p/4822099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!