zoukankan      html  css  js  c++  java
  • 两个Service之间相互监视的实现

    在实际开发中可能需要用到两个Service相互监视的情况,本示例就是实现此功能以作参考。

    服务A:
     1 public class ServiceA extends Service {
     2 
     3 
     4     private static final String TAG = ServiceA.class.getSimpleName();
     5     MyBinder mBinder;
     6     MyServiceConnection mServiceConnection;
     7     PendingIntent mPendingIntent;
     8 
     9     @Override
    10     public void onCreate() {
    11         super.onCreate();
    12 
    13         if(mBinder==null)
    14         {
    15             mBinder=new MyBinder();
    16         }
    17         mServiceConnection=new MyServiceConnection();
    18     }
    19 
    20     @Override
    21     public int onStartCommand(Intent intent, int flags, int startId) {
    22         ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);
    23         mPendingIntent=PendingIntent.getService(this,0,intent,0);
    24         Notification.Builder builder=new Notification.Builder(this);
    25         builder.setTicker("守护服务A启动中")
    26                 .setContentText("我是来守护服务B的")
    27                 .setContentTitle("守护服务A")
    28                 .setSmallIcon(R.mipmap.ic_launcher)
    29                 .setContentIntent(mPendingIntent)
    30                 .setWhen(System.currentTimeMillis());
    31         Notification notification=builder.build();
    32 
    33         startForeground(startId,notification);
    34 
    35 
    36         return START_STICKY;
    37     }
    38 
    39     @Override
    40     public IBinder onBind(Intent intent) {
    41         return mBinder;
    42     }
    43 
    44     public class MyBinder extends IBridgeInterface.Stub {
    45 
    46         @Override
    47         public String getName() throws RemoteException {
    48             return "name:"+TAG;
    49         }
    50     }
    51 
    52     class MyServiceConnection implements ServiceConnection {
    53 
    54         @Override
    55         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    56             String name=null;
    57             try {
    58                 name= IBridgeInterface.Stub.asInterface(iBinder).getName();
    59             } catch (RemoteException e) {
    60                 e.printStackTrace();
    61             }
    62 
    63 
    64             Toast.makeText(ServiceA.this,name+"连接成功",Toast.LENGTH_SHORT).show();
    65         }
    66 
    67         @Override
    68         public void onServiceDisconnected(ComponentName componentName) {
    69             Toast.makeText(ServiceA.this,TAG+"断开连接",Toast.LENGTH_SHORT).show();
    70 
    71             ServiceA.this.startService(new Intent(ServiceA.this,ServiceB.class));
    72 
    73             ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);
    74 
    75         }
    76     }
    77 
    78 
    79 }

    服务B:

     1 public class ServiceB extends Service {
     2 
     3     private static final String TAG = ServiceB.class.getSimpleName();
     4     private PendingIntent mPendingIntent;
     5     private MyBinder mBinder;
     6     private MyServiceConnection mServiceConnection;
     7 
     8     @Override
     9     public IBinder onBind(Intent intent) {
    10         return mBinder;
    11     }
    12 
    13     @Override
    14     public void onCreate() {
    15         super.onCreate();
    16         if (mBinder == null) {
    17             mBinder = new MyBinder();
    18         }
    19 
    20         mServiceConnection = new MyServiceConnection();
    21     }
    22 
    23     @Override
    24     public int onStartCommand(Intent intent, int flags, int startId) {
    25         this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
    26         mPendingIntent = PendingIntent.getService(this, 0, intent, 0);
    27         Notification.Builder builder = new Notification.Builder(this);
    28 
    29         builder.setTicker("守护服务B启动中")
    30                 .setContentText("我是来守护服务A的")
    31                 .setContentTitle("守护服务B")
    32                 .setSmallIcon(R.mipmap.ic_launcher)
    33                 .setContentIntent(mPendingIntent)
    34                 .setWhen(System.currentTimeMillis());
    35         Notification notification = builder.build();
    36         startForeground(startId, notification);
    37 
    38         return START_STICKY;
    39     }
    40 
    41     public class MyBinder extends IBridgeInterface.Stub {
    42 
    43         @Override
    44         public String getName() throws RemoteException {
    45             return "name:"+TAG;
    46         }
    47     }
    48 
    49     class MyServiceConnection implements ServiceConnection {
    50 
    51         @Override
    52         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    53             String name=null;
    54             try {
    55                 name=IBridgeInterface.Stub.asInterface(iBinder).getName();
    56             } catch (RemoteException e) {
    57                 e.printStackTrace();
    58             }
    59             Toast.makeText(ServiceB.this, name + "连接成功", Toast.LENGTH_SHORT).show();
    60         }
    61 
    62         @Override
    63         public void onServiceDisconnected(ComponentName componentName) {
    64             Toast.makeText(ServiceB.this, TAG + "断开连接", Toast.LENGTH_SHORT).show();
    65 
    66             ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class));
    67             ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
    68         }
    69     }
    70 
    71 
    72 }

    IBridgeInterface.aidl

    1 interface IBridgeInterface {
    2     String getName();
    3 }

    界面:

     1 public class MainActivity extends Activity {
     2 
     3 
     4     @Override
     5     protected void onCreate(Bundle savedInstanceState) {
     6         super.onCreate(savedInstanceState);
     7         setContentView(R.layout.activity_main);
     8 
     9         startService(new Intent(this, ServiceA.class));
    10         startService(new Intent(this, ServiceB.class));
    11     }
    12 
    13 }

    AndroidManifest.xml

    1         <service android:name=".services.ServiceA" />
    2         <service
    3             android:name=".services.ServiceB"
    4             android:process=":remote" />

    由于涉及到跨进程,onServiceConnected() 方法中使用

    IBridgeInterface.Stub.asInterface(iBinder).getName();

    而不能直接类型转换

    ((ServiceA.MyBinder)iBinder).getName();

    onStartCommand

    onStartCommand() 方法必须返回整型数。整型数是一个值,用于描述系统应该如何在服务终止的情况下继续运行服务。

    返回的值必须是以下常量之一:

    START_NOT_STICKY

      如果系统在 onStartCommand() 返回后终止服务,则除非有挂起 Intent 要传递,否则系统不会重建服务。

    START_STICKY

      如果系统在 onStartCommand() 返回后终止服务,则会重建服务并调用 onStartCommand(),但绝对不会重新传递最后一个 Intent。相反,除非有挂起 Intent 要启动服务(在这种情况下,将传递这些 Intent ),否则系统会通过空 Intent 调用 onStartCommand()。这适用于不执行命令、但无限期运行并等待作业的媒体播放器(或类似服务)。

    START_REDELIVER_INTENT

      如果系统在 onStartCommand() 返回后终止服务,则会重建服务,并通过传递给服务的最后一个 Intent 调用 onStartCommand()。任何挂起 Intent 均依次传递。这适用于主动执行应该立即恢复的作业(例如下载文件)的服务。

  • 相关阅读:
    Windows 2008 server + IIS 7 设置身份模拟(ASP.NET impersonation)
    记录windows操作系统启动日志
    C# 重启计算机的问题
    AcWing 1086 恨7不是妻
    AcWing 1084. 数字游戏 II
    AcWing 1083. Windy数
    golang学习笔记 生成JSON及解析JSON 清明
    WEB前端底层知识之浏览器是如何工作的(2)渲染引擎
    ASP.NET MVC Razor 输出没有编码的HTML字符串
    ext.grid的配置属性和方法
  • 原文地址:https://www.cnblogs.com/l2rf/p/5955530.html
Copyright © 2011-2022 走看看