• APK:强制apk栈顶


    需求:

    1、每隔60秒判断当前apk是否在当前页面,否就拉起来

    2、收到开机广播,延迟启动服务

    MyBroadcast.java

    package com.gatsby.galway;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Handler;
    import android.util.Log;
    
    public class MyBroadcast extends BroadcastReceiver {
    
        Context mContext;
        static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
    
        Handler mhandler = new Handler();
    
        private Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.d("gatsby", "Handler  DelayRunable!");
                Intent startService = new Intent(mContext, BootService.class);
                mContext.startService(startService);
            }
        };
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            this.mContext = context;
            String action = intent.getAction();
    
            if (action.equals(BOOT_COMPLETED)) {
                Log.d("gatsby", "CrushBroadcast BOOT_COMPLETED -----------!!!!");
                mhandler.postDelayed(runnable, 120000);
            }
        }
    
    }

    BootService.java

    package com.gatsby.galway;
    
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class BootService extends Service {
    
        private String TAG = "gatsby";
        private String outPackageInfo;
        private String PACKAGE_NAME = "com.example.myfirstapplication";
        private String PACKAGE_ACTIVITY = "com.example.myfirstapplication.activity.LoginActivity";
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            Log.d(TAG, "onCreate");
    
            MyTimerTask timerTask = new MyTimerTask();
            Timer timer = new Timer(true);
            timer.schedule(timerTask, 0, 60000);
        }
    
        public class MyTimerTask extends TimerTask {
            @Override
            public void run() {
                String packageInfo = RootCommand("dumpsys window | grep mCurrentFocus");
                if (!(packageInfo.contains(PACKAGE_NAME))) {
                    ApplicationLauncher(PACKAGE_NAME, PACKAGE_ACTIVITY);
                }
            }
        }
    
        private boolean ApplicationLauncher(String PackageName, String ActivityName) {
            ComponentName componentName = new ComponentName(PackageName, ActivityName);
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setComponent(componentName);
            if (getApplication().getPackageManager().resolveActivity(intent, 0) == null) {
                return false; // no this activity
            }
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            getApplication().startActivity(intent);
            return true;
        }
    
        private String RootCommand(String cmd) {
            Process process = null;
            DataOutputStream os = null;
            DataInputStream is = null;
            try {
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                os.writeBytes(cmd + "
    ");
                os.writeBytes("exit
    ");
                os.flush();
    
                int aa = process.waitFor();
                is = new DataInputStream(process.getInputStream());
    
                byte[] buffer = new byte[is.available()];
                is.read(buffer);
    
                outPackageInfo = new String(buffer);
                Log.d(TAG, outPackageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                }
            }
            return outPackageInfo;
        }
    
    }
  • 相关阅读:
    Creating fields using CSOM
    WCF Misconfiguration: Security Not Enabled
    WCF Misconfiguration: Insufficient Audit Failure Handling
    ASP.NET Misconfiguration: Excessive Session Timeout
    ASP.NET Misconfiguration: Missing Error Handling
    ASP.NET Misconfiguration: Request Validation Disabled
    ASP.NET Misconfiguration: Debug Information
    MIME Sniffing
    web.config中的HttpCookie.HttpOnly属性
    How to: Convert Between Various String Types
  • 原文地址:https://www.cnblogs.com/crushgirl/p/13254620.html
Copyright © 2020-2023  润新知