1.1、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gatsby.unlod"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <!--隐藏apk应用图标--> <data android:host="akm.app" android:pathPrefix="/openwith" android:scheme="myapp" /> </intent-filter> </activity> <receiver android:name="com.gatsby.unlod.CrushBroadcast"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name="com.gatsby.unlod.BootService"></service> </application> </manifest>
1.2、MainActivity .java
package com.gatsby.unlod; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Intent intent = new Intent(MainActivity.this, BootService.class); //startService(intent); } }
1.3、CrushBroadcast.java
package com.gatsby.unlod; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class CrushBroadcast extends BroadcastReceiver { static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (action.equals(BOOT_COMPLETED)) { Intent startService = new Intent(context, BootService.class); Log.d("gatsby", "CrushBroadcast-----------!!!!"); context.startService(startService); } } }
1.4、AppInfo.java
package com.gatsby.unlod; public class AppInfo { String packageName; public AppInfo() { } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } public AppInfo(String packageName) { this.packageName = packageName; } }
1.5、BootService .java
package com.gatsby.unlod; import android.app.Service; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; import java.io.DataInputStream; import java.io.DataOutputStream; import java.util.ArrayList; import java.util.List; public class BootService extends Service { List<AppInfo> appInfos = null; static boolean InstallApkEnable ; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("gatsby", "Service onCreate!"); loadAppReInstall(); } public void loadAppReInstall() { appInfos = getAppInfos(); for (AppInfo appInfo : appInfos) { //Log.d("gatsby", "appInfo.packageName->" + appInfo.packageName); if (appInfo.packageName.equals("com.hwzn.kanshousuo")) { InstallApkEnable = false; } else { InstallApkEnable = true; } } if (InstallApkEnable) { RootCommand("pm install /system/preinstall/InScreen/InScreen.apk"); } } public List<AppInfo> getAppInfos() { PackageManager pm = getApplication().getPackageManager(); List<PackageInfo> packgeInfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); appInfos = new ArrayList<AppInfo>(); for (PackageInfo packgeInfo : packgeInfos) { if ((packgeInfo.applicationInfo.flags & packgeInfo.applicationInfo.FLAG_SYSTEM) != 0) { continue; } String packageName = packgeInfo.packageName; AppInfo appInfo = new AppInfo(packageName); //Log.d("gatsby", "packageName->" + packageName); appInfos.add(appInfo); } return appInfos; } private void RootCommand(String cmd) { Process process = null; DataOutputStream os = null; DataInputStream is = null; try { process = Runtime.getRuntime().exec("/system/xbin/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); String out = new String(buffer); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (is != null) { is.close(); } process.destroy(); } catch (Exception e) { } } } }