关于进程守护,拉起问题
来源:互联网 发布:nba数据查询 编辑:程序博客网 时间:2024/04/29 23:32
之前看了一篇文章说的是进程的守护问题,忘记实在哪里看了,现在有时间吧代码发出来,做下笔记
这一块的问题 常见于推送相关的,因为有的时候系统会把,开启的服务给kill 了,但是服务不会自动启动
1.aidl 文件
// IProcessService.aidlpackage com.stevefat.myapplication;// Declare any non-default types here with import statementsinterface IProcessService { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ String getServiceName();}
2.第一个服务
package com.stevefat.myapplication.services;import android.app.Service;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;import com.stevefat.myapplication.IProcessService;/** * Author: ngh * date: 2016/10/9 */public class DaemonService extends Service { String TAG = "DaemonService"; private DaemonBinder mDaemonBinder; private DaemonServiceConnection mDaemonServiceConn; public DaemonService() { } @Override public void onCreate() { super.onCreate(); mDaemonBinder = new DaemonBinder(); if (mDaemonServiceConn == null) { mDaemonServiceConn = new DaemonServiceConnection(); } Log.e(TAG, TAG + " onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); Log.e(TAG, TAG + " onStartCommand"); bindService(new Intent(this, RemoteService.class), mDaemonServiceConn, Context.BIND_IMPORTANT); return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return mDaemonBinder; } /** * 通过AIDL实现进程间通信 */ class DaemonBinder extends IProcessService.Stub { @Override public String getServiceName() throws RemoteException { return TAG; } } /** * 连接远程服务 */ class DaemonServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { try { // 与远程服务通信 IProcessService process = IProcessService.Stub.asInterface(service); Log.e(TAG, "连接" + process.getServiceName() + "服务成功"); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // RemoteException连接过程出现的异常,才会回调,unbind不会回调 startService(new Intent(DaemonService.this, RemoteService.class)); bindService(new Intent(DaemonService.this, RemoteService.class), mDaemonServiceConn, Context.BIND_IMPORTANT); } }}
3.第二个服务
package com.stevefat.myapplication.services;import android.app.Service;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;import com.stevefat.myapplication.IProcessService;/** * Author: ngh * date: 2016/10/9 */public class RemoteService extends Service { String TAG = "RemoteService"; private ServiceBinder mServiceBinder; private RemoteServiceConnection mRemoteServiceConn; @Override public void onCreate() { super.onCreate(); mServiceBinder = new ServiceBinder(); if (mRemoteServiceConn == null) { mRemoteServiceConn = new RemoteServiceConnection(); } Log.e(TAG, TAG + " onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); Log.e(TAG, TAG + " onStartCommand"); bindService(new Intent(this, DaemonService.class), mRemoteServiceConn, Context.BIND_IMPORTANT); return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return mServiceBinder;} /** * 通过AIDL实现进程间通信 */ class ServiceBinder extends IProcessService.Stub { @Override public String getServiceName() throws RemoteException { return "RemoteService"; } } /** * 连接远程服务 */ class RemoteServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { try { IProcessService process = IProcessService.Stub.asInterface(service); Log.e(TAG, "连接" + process.getServiceName() + "服务成功"); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // RemoteException连接过程出现的异常,才会回调,unbind不会回调 startService(new Intent(RemoteService.this, DaemonService.class)); bindService(new Intent(RemoteService.this, DaemonService.class), mRemoteServiceConn, Context.BIND_IMPORTANT); } }}
4.在配置清单文件中添加两个服务
<service android:name=".services.DaemonService"/><service android:name=".services.RemoteService"/>
5.在activity 中启动这两个服务
Intent intent = new Intent(MainActivity.this,DaemonService.class); startService(intent); Intent intent1 = new Intent(MainActivity.this,RemoteService.class); startService(intent1);
0 0
- 关于进程守护,拉起问题
- 关于守护进程deamon
- 关于守护进程的
- 关于守护进程
- 关于守护进程daemon
- 关于守护进程linuxc
- 关于linux的守护进程
- 关于守护进程的解析
- 守护进程及守护进程中两次fork问题
- 总结]关于守护进程解释文章
- 关于R3下的自身进程守护
- 关于linux端php实现守护进程
- 关于守护进程的一点感悟
- 守护进程 关于setsid() 函数的说明
- 守护进程以及fork两次的问题
- 守护进程守护自身
- 守护线程、守护进程
- 拉起子进程后端口被继承
- HTTP HTTPS WebService
- iOS Runtime 运行时之二:成员和属性,关联对象
- ubuntu解压命令全览
- 表和视图,存储过程和函数的区别
- OC 执行JS中的实例方法的那些坑
- 关于进程守护,拉起问题
- video标签-点击封面 播放视频,显示进度条
- DAY25:leetcode #53 Maximum Subarray
- 电子元器件分类
- Python之字符串方法find( )
- Struts2 中result type属性说明
- iOS性能调试
- 解决MFC不能进行多文件选择的问题
- 找出一串数字中的最大和子串