zoukankan      html  css  js  c++  java
  • 粘性Service

    粘性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
  • 相关阅读:
    双主MySQL+keepalived高可用配置
    centos6.8服务器部署svn
    Centos6下rpm安装MySQL5.6
    CentOS6.8下部署Zabbix3.0
    python核心编程第六章练习6-15
    python核心编程第六章练习6-14
    scp 将数据从一台linux服务器复制到另一台linux服务器
    $config['base_url'] BASE_URL
    ubunt设置终端快捷键设置 及 常用快捷键
    URL 路由
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4822099.html
Copyright © 2011-2022 走看看