天天看點

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();

}

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);

}

}

}

[email protected](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程序保活的一般套路