AndroidApp唤醒⼂保活详解,以及代码展⽰
安卓进程进程保活分为:
⿊⾊保活,⽩⾊保活,灰⾊保活
⿊⾊保活:
可以说⿊⾊保活,可以通过⽹络切换,拍照,拍视频,开机,利⽤系统产⽣的⼴播唤醒app,接⼊三⽅的sdk也会唤醒⼀些app,如⽀付宝,..........这样的话,这样的话,不敢想象系统存活会给系统带来多⼤的负担,所以我们的安卓⼿机也变得卡了,google官⽅可能也认识了这么⼀点,所以取消了
ACTION_NEW_PICTURE(拍照),ACTION_NEW_VIDEO(拍视频),CONNECTIVITY_ACTION(⽹络切换)
app也会随着做⼀点改变,(不过sdk的使⽤还是会通过⼀个app启动相关的⼀些app , ⿊⾊保活我个⼈认为不推荐使⽤,毕竟为了我们⼴⼤安卓⽤户。)
⽩⾊保活:
⽩⾊保活⼿段⾮常简单,就是调⽤系统api启动⼀个前台的Service进程,这样会在系统的通知栏⽣成⼀个Notification,⽤来让⽤户知道有这样⼀个app在运⾏着,哪怕当前的app退到了后台。
不过⽤户看到这个图标的时候,都会把它清空的。。。。
灰⾊保活:
可以说,灰⾊保活是⽤的最多,当⽤户不知不觉中这个app程序已经在后台运⾏了。
它是利⽤系统的漏洞来启动⼀个前台的Service进程,与普通的启动⽅式区别在于,它不会在系统通知栏处出现⼀个Notification,看起来就如同运⾏着⼀个后台Service进程⼀样。这样做带来的好处就是,
⽤户⽆法察觉到你运⾏着⼀个前台进程(因为看不到Notification),但你的进程优先级⼜是⾼于普通后台进程的。API < 18,启动前台Service时直接传⼊new Notification();API >= 18,同时启动两个id相同的前台Service,然后再将后启动的Service做stop处理;
安卓app唤醒:
其实app唤醒的介绍很好说,app唤醒就是当打开⼀个app的时候,另⼀个app⾥有对应刚打开那个app的属性标志,根据你想要的唤醒⽅式,执⾏不同的代码操作,这样就可以唤醒另⼀个没打开的app了。(代码在最下⾯)
下⾯我展⽰⼀下这⼏种状态下的代码:
这个是xml布局,主要是为了展⽰我所介绍的⼏种保活⽅式:
<LinearLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/mBtn_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="⽩⾊保活" />
<Button
android:id="@+id/mBtn_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="灰⾊保活" />
<Button
android:id="@+id/mBtn_black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="⿊⾊保活(发⼴播)" />
<Button
android:id="@+id/mBtn_background_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通后台 Service 进程" />
</LinearLayout>
下⾯是主要实现类:
WakeReceiver
import android.app.Notification;
import android.app.Service;
t.BroadcastReceiver;
t.Context;安卓intent用法
t.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
public class WakeReceiver extends BroadcastReceiver {
private final static String TAG = SimpleName(); private final static int WAKE_SERVICE_ID = -1111;
/**
* 灰⾊保活⼿段唤醒⼴播的action
*/
public final static String GRAY_WAKE_ACTION = "ay";
@Override
public void onReceive(Context context, Intent intent) {
String action = Action();
if (GRAY_WAKE_ACTION.equals(action)) {
Log.i(TAG, "wake !! wake !! ");
Intent wakeIntent = new Intent(context, WakeNotifyService.class); context.startService(wakeIntent);
}
}
/**
* ⽤于其他进程来唤醒UI进程⽤的Service
*/
public static class WakeNotifyService extends Service {
@Override
public void onCreate() {
Log.i(TAG, "WakeNotifyService->onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "WakeNotifyService->onStartCommand");
if (Build.VERSION.SDK_INT < 18) {
startForeground(WAKE_SERVICE_ID, new Notification());//API < 18 ,此⽅法能有效隐藏Notification上的图标 } else {
Intent innerIntent = new Intent(this, WakeGrayInnerService.class);
startService(innerIntent);
startForeground(WAKE_SERVICE_ID, new Notification());
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(TAG, "WakeNotifyService->onDestroy");
}
}
/**
* 给 API >= 18 的平台上⽤的灰⾊保活⼿段
*/
public static class WakeGrayInnerService extends Service {
@Override
public void onCreate() {
Log.i(TAG, "InnerService -> onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "InnerService -> onStartCommand");
startForeground(WAKE_SERVICE_ID, new Notification());
//stopForeground(true);
stopSelf();
StartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
/
/ TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(TAG, "InnerService -> onDestroy");
}
}
}
BackGroundService
import android.app.Service;
t.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* 普通的后台Service进程
*
* @author clock
* @since 2016-04-12
*/
public class BackgroundService extends Service {
private final static String TAG = SimpleName();
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
}
}
GrayService
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
t.Context;
t.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
iver.WakeReceiver;
/**
* 灰⾊保活⼿法创建的Service进程
*
* @author Clock
* @since 2016-04-12
*/
*/
public class GrayService extends Service {
private final static String TAG = SimpleName();
/**
* 定时唤醒的时间间隔,5分钟
*/
private final static int ALARM_INTERVAL = 5 * 60 * 1000;
private final static int WAKE_REQUEST_CODE = 6666;
private final static int GRAY_SERVICE_ID = -1001;
@Override
public void onCreate() {
Log.i(TAG, "GrayService->onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "GrayService->onStartCommand");
if (Build.VERSION.SDK_INT < 18) {
startForeground(GRAY_SERVICE_ID, new Notification());//API < 18 ,此⽅法能有效隐藏Notification上的图标
} else {
Intent innerIntent = new Intent(this, GrayInnerService.class);
startService(innerIntent);
startForeground(GRAY_SERVICE_ID, new Notification());
}
//发送唤醒⼴播来促使挂掉的UI进程重新启动起来
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent();
alarmIntent.setAction(WakeReceiver.GRAY_WAKE_ACTION);
PendingIntent operation = Broadcast(this, WAKE_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ALARM_INTERVAL, operation);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(TAG, "GrayService->onDestroy");
}
/**
* 给 API >= 18 的平台上⽤的灰⾊保活⼿段
*/
public static class GrayInnerService extends Service {
@Override
public void onCreate() {
Log.i(TAG, "InnerService -> onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "InnerService -> onStartCommand");
startForeground(GRAY_SERVICE_ID, new Notification());
//stopForeground(true);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论