进程包活

因为ios在杀死app后都可以接收到通知,所以不懂的人就会觉得那Android应该都是可以的啊,

解决方法:

1、比如说什么搞两个service互相拉起,不可以

2、APP杀死,收不到极光推送,网上说在mainfest注册自己的 recevice时,写入这两个就可以

<action android:name="android.intent.action.BOOT_COMPLETED" /><!--开机广播-->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><!--网络状态改变广播-->

不可以。

3、还有好多人说可以在mainfest加入

<!--   android:persistent="true" //,持续的,一直的,这样的话,app是杀不死的,
推送肯是强烈建议不要这样做,因为这样就像某些流氓软件一样了,毕竟我定有可以收到了。
但们做个应用出来,也不想让别人以为我们的是流氓软件吧-->

不可以。

4、还有说加这个的

<intent-filter android:priority="1000">

网上有人在说:

//自定义的接收器
public class BoardcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
     Intent pushintent=new Intent(context,PushService.class);//启动极光推送的服务
        context.startService(pushintent);
    }
}


//在mainfest中 静态注册接收器
  <receiver
            android:name="BoardcastReceiver"
            android:enabled="true">
            <intent-filter>
                <!--Required 用户注册SDK的intent-->
                <action android:name="cn.jpush.android.intent.REGISTRATION" />
                <!--Required 用户接收SDK消息的intent-->
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                <!--Required 用户接收SDK通知栏信息的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                <!--Required 用户打开自定义通知栏的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <action android:name="cn.jpush.android.intent.CONNECTION" />
                <action android:name="android.intent.action.BOOT_COMPLETED"/><!--开机广播-->
                <action android:name="

android.net.conn.CONNECTIVITY_CHANGE"/><!--网络状态改变广播-->
                <category android:name="com.woman.RCTest" />
            </intent-filter>
        </receiver>

vivo是可以收到,小米华为三星不可以

双进程守护service保活的策略:

创建俩个service利用aidl进程间通讯,进行包活策略:

在文件夹下定义Guard.aidl文件:

package com.guardservice.aidl;
interface GuardAidl{
    void doSomething();
}

本地服务LocalService.java:

package com.guardservice;
 
import com.guardservice.aidl.GuardAidl;
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.IBinder;
import android.os.RemoteException;
import android.util.Log;
 
public class LocalService extends Service {
 
	private MyBilder mBilder;
 
	@Override
	public IBinder onBind(Intent intent) {
		if (mBilder == null)
			mBilder = new MyBilder();
 
		return mBilder;
 
	}
 
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
 
	}
 
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		this.bindService(new Intent(LocalService.this, RemoteService.class),
				connection, Context.BIND_ABOVE_CLIENT);
 
		Notification notification = new Notification(R.drawable.ic_launcher,
				"安全服务启动中", System.currentTimeMillis());
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
				0);
		notification.setLatestEventInfo(this, "安全服务", "安全服务...", pendingIntent);
		startForeground(startId, notification);
		
		return START_STICKY;
	}
 
	private class MyBilder extends GuardAidl.Stub {
 
		@Override
		public void doSomething() throws RemoteException {
			Log.i("TAG", "绑定成功!");
			Intent localService = new Intent(LocalService.this,
					RemoteService.class);
			LocalService.this.startService(localService);
			LocalService.this
					.bindService(new Intent(LocalService.this,
							RemoteService.class), connection,
							Context.BIND_ABOVE_CLIENT);
		}
 
	}
 
	private ServiceConnection connection = new ServiceConnection() {
 
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i("TAG", "RemoteService被杀死了");
			Intent localService = new Intent(LocalService.this,
					RemoteService.class);
			LocalService.this.startService(localService);
			LocalService.this
					.bindService(new Intent(LocalService.this,
							RemoteService.class), connection,
							Context.BIND_ABOVE_CLIENT);
		}
 
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i("TAG", "RemoteService链接成功!");
			try {
				if (mBilder != null)
					mBilder.doSomething();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	};
 
}

远程服务RemoteService.java:

package com.guardservice;
 
import com.guardservice.aidl.GuardAidl;
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.IBinder;
import android.os.RemoteException;
import android.util.Log;
 
public class RemoteService extends Service {
 
	private MyBilder mBilder;
	//private final static int GRAY_SERVICE_ID = 1002;
 
	@Override
	public IBinder onBind(Intent intent) {
		if (mBilder == null)
			mBilder = new MyBilder();
		return mBilder;
	}
 
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
 
	}
 
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// 在RemoteService运行后,我们对LocalService进行绑定。 把优先级提升为前台优先级
		this.bindService(new Intent(RemoteService.this, LocalService.class),
				connection, Context.BIND_ABOVE_CLIENT);
 
		Notification notification = new Notification(R.drawable.ic_launcher,
				"安全服务启动中", System.currentTimeMillis());
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
				0);
		notification.setLatestEventInfo(this, "安全服务", "安全服务...", pendingIntent);
		startForeground(startId, notification);
		return START_STICKY;
	}
 
	private class MyBilder extends GuardAidl.Stub {
 
		@Override
		public void doSomething() throws RemoteException {
			Log.i("TAG", "绑定成功!");
		}
 
	}
 
	private ServiceConnection connection = new ServiceConnection() {
 
		/**
		 * 在终止后调用,我们在杀死服务的时候就会引起意外终止,就会调用onServiceDisconnected
		 * 则我们就得里面启动被杀死的服务,然后进行绑定
		 */
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i("TAG", "LocalService被杀死了");
			Intent remoteService = new Intent(RemoteService.this,
					LocalService.class);
			RemoteService.this.startService(remoteService);
			RemoteService.this.bindService(new Intent(RemoteService.this,
					LocalService.class), connection, Context.BIND_ABOVE_CLIENT);
		}
 
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i("TAG", "LocalService链接成功!");
			try {
				if (mBilder != null)
					mBilder.doSomething();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	};
 
}

基本不会被杀死。资料来自网上

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值