APK: 开机自启

1.1.AndroidMainfest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xinhua.bootservice"
    android:versionCode="1"
    android:versionName="1.0" >
 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name = "android.permission.GET_TASKS"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.xinhua.bootservice.MyBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
         
        <service android:name="com.xinhua.bootservice.BootService" ></service>
    </application>
 
</manifest>

1.2.MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xinhua.bootservice"
    android:versionCode="1"
    android:versionName="1.0" >
 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name = "android.permission.GET_TASKS"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.xinhua.bootservice.MyBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
         
        <service android:name="com.xinhua.bootservice.BootService" ></service>
    </application>
 
</manifest>  

1.4.BootService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
    }
 
}

  

 

posted @   CrushGirl  阅读(741)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示