双守护进程保护程序运行

涉及到服务通信,使用AIDL。

MyProcess.aidl:

package com.study.dn_process.inter;
interface MyProcess{
	String getProcessName();
}

LocalService:

package com.study.dn_process.service;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class LocalService extends Service {

	private MyBinder binder;

	private MyServiceConnection conn;

	private PendingIntent pendingIntent;

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (binder == null) {
			binder = new MyBinder();
		}
		conn = new MyServiceConnection();
	}

	@SuppressWarnings("deprecation")
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// LocalService.this.startService(new Intent(LocalService.this,
		// RemoteService.class));

		// 绑定远程服务
		this.bindService(new Intent(LocalService.this, RemoteService.class),
				conn, Context.BIND_IMPORTANT);

		Notification notification = new Notification(R.drawable.ic_launcher,
				"服务启动中", System.currentTimeMillis());
		pendingIntent = PendingIntent.getService(this, 0, intent, 0);
		notification.setLatestEventInfo(this, "服务", "---", pendingIntent);
		return START_STICKY;
	}

	class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.e("info", "ServiceConnected");
		}

		@SuppressLint("InlinedApi")
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// 重新连接远程服务
			LocalService.this.startService(new Intent(LocalService.this,
					RemoteService.class));
			LocalService.this.bindService(new Intent(LocalService.this,
					RemoteService.class), conn, Context.BIND_IMPORTANT);
		}

	}

	class MyBinder extends MyProcess.Stub {

		@Override
		public String getProcessName() throws RemoteException {
			return "LocalService";
		}
	}
}


RemoteService:

package com.study.dn_process.service;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class RemoteService extends Service {

	private MyBinder binder;

	private MyServiceConnection conn;

	private PendingIntent pendingIntent;

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		if (binder == null) {
			binder = new MyBinder();
		}
		conn = new MyServiceConnection();
	}

	@SuppressWarnings("deprecation")
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// RemoteService.this.startService(new Intent(RemoteService.this,
		// LocalService.class));
		this.bindService(new Intent(RemoteService.this, LocalService.class),
				conn, Context.BIND_IMPORTANT);

		Notification notification = new Notification(R.drawable.ic_launcher,
				"服务启动中", System.currentTimeMillis());
		pendingIntent = PendingIntent.getService(this, 0, intent, 0);
		notification.setLatestEventInfo(this, "服务", "---", pendingIntent);

		return START_STICKY;
	}

	class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.e("info", "ServiceConnected");
		}

		@SuppressLint("InlinedApi")
		@Override
		public void onServiceDisconnected(ComponentName name) {
			RemoteService.this.startService(new Intent(RemoteService.this,
					LocalService.class));
			RemoteService.this.bindService(new Intent(RemoteService.this,
					LocalService.class), conn, Context.BIND_IMPORTANT);
		}

	}

	class MyBinder extends MyProcess.Stub {

		@Override
		public String getProcessName() throws RemoteException {
			return "RemoteService";
		}
	}
}


MainActivity:

package com.study.dn_process;

import com.study.dn_process.service.LocalService;
import com.study.dn_process.service.RemoteService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.startService(new Intent(this, LocalService.class));
		this.startService(new Intent(this, RemoteService.class));
	}

}

tip:清单文件需要注册!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值