android 程序长效保活,Android应用自启与保活

在Android开发中有些应用需要让应用的服务在后台运行不被杀死。下面介绍两种方法。但是这两种方法一起用只能拉活7.0以下的系统上的应用。

一,使用开锁屏广播拉活。

在模拟器上面所有版本的Android系统在进程被杀死后都可以接受到开锁屏广播。

真正的手机上面,小于等于5.0的手机在进程被杀死后可以接受到开锁屏广播。大于5.0在进程被杀死后就不能接受开锁屏广播了。

二,使用JobService保活。

JobService在5.0 5.1和6.0的手机可以起到服务拉活的作用。之后的版本就不能起效果了。包括模拟器测试效果一样。对于7.0 7.1和8.0 8.1的手机实现服务保活目前就没有办法了。qq和微信和手机生产厂商有合作所有能被拉活,在设置为允许自启的情况下。

一,使用开锁屏广播拉活

1,在AndroidManifest里面添加广播的过滤标签。

2,创建ScreenReceiver广播接受类。在接受广播后拉活服务。

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import com.example.admin.jobservices.StaticMethodTool;

/**

* 开锁屏广播

*/

public class ScreenReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

StaticMethodTool.starSer(context);

}

}

二,使用JobService保活

MyJobService类的实现

import android.annotation.TargetApi;

import android.app.job.JobInfo;

import android.app.job.JobParameters;

import android.app.job.JobScheduler;

import android.app.job.JobService;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.os.Build;

import android.util.Log;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public class MyJobService extends JobService {

private int kJobId = 0;

@Override

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

scheduleJob(getJobInfo());

return START_NOT_STICKY;

}

@Override

public boolean onStartJob(JobParameters params) {

Log.e("JOBSERVICE", "onStartJob +++");

String sName = WorkService.class.getName();

boolean isLocalServiceWork = StaticMethodTool.isServiceRunning(sName, this);//此处填入服务的全名

if (!isLocalServiceWork) {

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

}

return true;

}

@Override

public boolean onStopJob(JobParameters params) {

Log.e("JOBSERVICE", "onStopJob +++");

scheduleJob(getJobInfo());

return true;

}

//将任务作业发送到作业调度中去

public void scheduleJob(JobInfo t) {

JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

tm.schedule(t);

}

public JobInfo getJobInfo() {

JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, MyJobService.class));

builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);

// builder.setPersisted(true);//手机重启

builder.setRequiresCharging(false);

builder.setRequiresDeviceIdle(false);

//间隔1000毫秒

builder.setPeriodic(1000);

return builder.build();

}

}

用于处理事件的服务WorkService

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

/**

* 用于正式运行处理事件的服务

*/

public class WorkService extends Service {

long i = 0L;

public void onCreate() {

super.onCreate();

i = 0;

new Thread() {

@Override

public void run() {

super.run();

while (true) {

try {

Thread.sleep(1000);

} catch (Exception e) {

}

Log.e("JOBSERVICE", "WorkService 服务没有死" + (i++));

}

}

}.start();

}

@Override

public IBinder onBind(Intent intent) {

return null;

}

}

AndroidManifest里面的服务标签配置

android:name="com.example.admin.jobservices.WorkService"

android:enabled="true"

android:exported="true" />

android:name="com.example.admin.jobservices.MyJobService"

android:permission="android.permission.BIND_JOB_SERVICE"

/>

启动MyJobService示例代码

import android.app.ActivityManager;

import android.content.Context;

import android.content.Intent;

import android.os.Build;

import android.util.Log;

import java.util.List;

/**

* Created by lenovo on 2018/6/19.

* 公共方法。

*/

public class StaticMethodTool {

/**

* 启动服务

*

* @param mAct

*/

public static void starSer(Context mAct) {

if (Build.VERSION.SDK_INT > 20 && Build.VERSION.SDK_INT < 24) {//JobService只支持21,22,23 也就是5.0 5.1 6.0的系统

if (!isServiceRunning("" + WorkService.class.getName(), mAct)) {

Intent intent1 = new Intent(mAct, MyJobService.class);

intent1.setAction("myaction");

mAct.startService(intent1);

} else {

Log.e("JOBSERVICE", "5.0以上手机版本 服务运行中+++");

}

} else {

if (!isServiceRunning(WorkService.class.getName(), mAct)) {

Intent intent1 = new Intent(mAct, WorkService.class);

mAct.startService(intent1);

} else {

Log.e("JOBSERVICE", "5.1以下的手机版本 服务运行中+++");

}

}

}

/**

* 判断服务是否处于运行状态.

*

* @param servicename

* @param context

* @return

*/

public static boolean isServiceRunning(String servicename, Context context) {

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

List infos = am.getRunningServices(100);

for (ActivityManager.RunningServiceInfo info : infos) {

if (servicename.equals(info.service.getClassName())) {

return true;

}

}

return false;

}

}

MainActiviy类

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

RelativeLayout rlParents = new RelativeLayout(this.getApplicationContext());

TextView tv = new TextView(this.getApplicationContext());

tv.setText("Hello MainActivity");

tv.setTextSize(50);

rlParents.addView(tv);

setContentView(rlParents);

Button buttonTiao = new Button(this.getApplicationContext());

RelativeLayout.LayoutParams rlButton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

rlButton.addRule(RelativeLayout.CENTER_IN_PARENT);

rlParents.addView(buttonTiao, rlButton);

buttonTiao.setText("启动JobService");

buttonTiao.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//启动制定App的指定页面

StaticMethodTool.starSer(MainActivity.this);

}

});

}

}

应用保活与拉活Demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值