android x 进程保活_Android进程保活的一般套路

Android进程保活的一般套路

publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

publicclassLiveActivityextendsActivity{

publicstaticfinalStringTAG=LiveActivity.class.getSimpleName();

publicstaticvoidactionToLiveActivity(ContextpContext){

Intentintent=newIntent(pContext,LiveActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

pContext.startActivity(intent);

}

@OverrideprotectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

Log.d(TAG,"onCreate");

setContentView(R.layout.activity_live);

Windowwindow=getWindow();

//放在左上角

window.setGravity(Gravity.START-Gravity.TOP);

WindowManager.LayoutParamsattributes=window.getAttributes();

//宽高设计为1个像素

attributes.width=1;

attributes.height=1;

//起始坐标

attributes.x=0;

attributes.y=0;

window.setAttributes(attributes);

ScreenManager.getInstance(this).setActivity(this);

}

@OverrideprotectedvoidonDestroy(){

super.onDestroy();

Log.d(TAG,"onDestroy");

}

}

true

@android:color/transparent

@null

true

publicclassScreenBroadcastListener{

privateContextmContext;

privateScreenBroadcastReceivermScreenReceiver;

privateScreenStateListenermListener;

publicScreenBroadcastListener(Contextcontext){

mContext=context.getApplicationContext();

mScreenReceiver=newScreenBroadcastReceiver();

}

interfaceScreenStateListener{

voidonScreenOn();

voidonScreenOff();

}

/**

* screen状态广播接收者

*/

privateclassScreenBroadcastReceiverextendsBroadcastReceiver{

privateStringaction=null;

@OverridepublicvoidonReceive(Contextcontext,Intentintent){

action=intent.getAction();

if(Intent.ACTION_SCREEN_ON.equals(action)){// 开屏

mListener.onScreenOn();

}elseif(Intent.ACTION_SCREEN_OFF.equals(action)){// 锁屏

mListener.onScreenOff();

}

}

}

publicvoidregisterListener(ScreenStateListenerlistener){

mListener=listener;

registerListener();

}

privatevoidregisterListener(){

IntentFilterfilter=newIntentFilter();

filter.addAction(Intent.ACTION_SCREEN_ON);

filter.addAction(Intent.ACTION_SCREEN_OFF);

mContext.registerReceiver(mScreenReceiver,filter);

}

}

publicclassScreenManager{

privateContextmContext;

privateWeakReferencemActivityWref;

publicstaticScreenManagergDefualt;

publicstaticScreenManagergetInstance(ContextpContext){

if(gDefualt==null){

gDefualt=newScreenManager(pContext.getApplicationContext());

}

returngDefualt;

}

privateScreenManager(ContextpContext){

this.mContext=pContext;

}

publicvoidsetActivity(ActivitypActivity){

mActivityWref=newWeakReference(pActivity);

}

publicvoidstartActivity(){

LiveActivity.actionToLiveActivity(mContext);

}

publicvoidfinishActivity(){

//结束掉LiveActivity

if(mActivityWref!=null){

Activityactivity=mActivityWref.get();

if(activity!=null){

activity.finish();

}

}

}

}

publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

finalScreenManagerscreenManager=ScreenManager.getInstance(MainActivity.this);

ScreenBroadcastListenerlistener=newScreenBroadcastListener(this);

listener.registerListener(newScreenBroadcastListener.ScreenStateListener(){

@Override

publicvoidonScreenOn(){

screenManager.finishActivity();

}

@Override

publicvoidonScreenOff(){

screenManager.startActivity();

}

});

}

}

public class LiveService extends Service {

public static void toLiveService(Context pContext){

Intent intent=new Intent(pContext,LiveService.class);

pContext.startService(intent);

}

@Nullable

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

//屏幕关闭的时候启动一个1像素的Activity,开屏的时候关闭Activity

final ScreenManager screenManager = ScreenManager.getInstance(LiveService.this);

ScreenBroadcastListener listener = new ScreenBroadcastListener(this);

listener.registerListener(new ScreenBroadcastListener.ScreenStateListener() {

@Override

public void onScreenOn() {

screenManager.finishActivity();

}

@Override

public void onScreenOff() {

screenManager.startActivity();

}

});

return START_REDELIVER_INTENT;

}

}

android:process=":live_service"/>

Process.killProcessQuiet(pid);

Process.killProcessQuiet(app.pid);

Process.killProcessGroup(app.info.uid, app.pid);

public class KeepLiveService extends Service {

public static final int NOTIFICATION_ID=0x11;

public KeepLiveService() {

}

@Override

public IBinder onBind(Intent intent) {

throw new UnsupportedOperationException("Not yet implemented");

}

@Override

public void onCreate() {

super.onCreate();

//API 18以下,直接发送Notification并将其置为前台

if (Build.VERSION.SDK_INT

startForeground(NOTIFICATION_ID, new Notification());

} else {

//API 18以上,发送Notification并将其置为前台后,启动InnerService

Notification.Builder builder = new Notification.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher);

startForeground(NOTIFICATION_ID, builder.build());

startService(new Intent(this, InnerService.class));

}

}

public static class InnerService extends Service{

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onCreate() {

super.onCreate();

//发送与KeepLiveService中ID相同的Notification,然后将其取消并取消自己的前台显示

Notification.Builder builder = new Notification.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher);

startForeground(NOTIFICATION_ID, builder.build());

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

stopForeground(true);

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manager.cancel(NOTIFICATION_ID);

stopSelf();

}

},100);

}

}

}

JobSheduler@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public class MyJobService extends JobService {

@Override

public void onCreate() {

super.onCreate();

startJobSheduler();

}

public void startJobSheduler() {

try {

JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), MyJobService.class.getName()));

builder.setPeriodic(5);

builder.setPersisted(true);

JobScheduler jobScheduler = (JobScheduler) this.getSystemService(Context.JOB_SCHEDULER_SERVICE);

jobScheduler.schedule(builder.build());

} catch (Exception ex) {

ex.printStackTrace();

}

}

@Override

public boolean onStartJob(JobParameters jobParameters) {

return false;

}

@Override

public boolean onStopJob(JobParameters jobParameters) {

return false;

}

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

return START_REDELIVER_INTENT;

}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)

public class LiveService extends NotificationListenerService {

public LiveService() {

}

@Override

public void onNotificationPosted(StatusBarNotification sbn) {

}

@Override

public void onNotificationRemoved(StatusBarNotification sbn) {

}

}

android:name=".LiveService"

android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

来源: http://bbs.thankbabe.com/topic/257/android进程保活的一般套路

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值