APK: 开机自启


1.1.AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>


     
     
    

    
        
            
                

                
            
        
        
            
                
            
        
        
        
    

1.2.MainActivity.java

<?xml version="1.0" encoding="utf-8"?>


     
     
    

    
        
            
                

                
            
        
        
            
                
            
        
        
        
    

  

1.4.BootService.java

package com.xinhua.bootservice;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Calendar;

import java.util.TimerTask;

import com.android.xhapimanager.XHApiManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;

import android.os.IBinder;
import android.util.Log;

public class BootService extends Service {

    private String TAG = "gatsby";
    private String outPackageInfo;
    private String PACKAGE_NAME = "com.gongfubao.attendance";
    private String PACKAGE_ACTIVITY = "com.gongfubao.attendance.ui.StartActivity";
    // XHApiManager apimanager;

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

        // ApplicationLauncher(PACKAGE_NAME,PACKAGE_ACTIVITY);
        SetRtc_Time();
        // apimanager = new XHApiManager();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);

        /*
         * MyTimerTask timerTask = new MyTimerTask(); Timer timer = new Timer(true);
         * timer.schedule(timerTask, 0, 60000);// 定时每30秒执行一次
         */
    }

    public void SetRtc_Time() {

        Runnable run = new Runnable() {

            @Override
            public void run() {

                while (true) {
                    Calendar calendar = Calendar.getInstance();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    int minute = calendar.get(Calendar.MINUTE);
                    int miao = calendar.get(Calendar.SECOND);
                    // Log.d(TAG,"hour"+hour);
                    // Log.d(TAG,"minute"+minute);
                    // Log.d(TAG,"miao"+miao);
                    if (2 == hour && 59 == minute) {
                        // Log.d(TAG,"18==hour && 30==minute");
                        sendBroadcast(new Intent().setAction(""));
                        // apimanager.XHReboot();
                        // break;
                    }

                    try {
                        Thread.sleep(1000);// 设置间隔
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t1 = new Thread(run);
        t1.start();

    }

    public class MyTimerTask extends TimerTask {
        @Override
        public void run() {

            String packageInfo = RootCommand("dumpsys window | grep mCurrentFocus");
            Log.d(TAG, "packageInfo = " + packageInfo);
            if (!(packageInfo.contains(PACKAGE_NAME))) {
                Log.d(TAG, "PACKAGE_NAME============11111 ");
                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 + "\n");
            os.writeBytes("exit\n");
            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;
    }

}

相关