<?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>
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); } }
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); } } }
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; } }
package com.gatsby.unlod; import android.app.Service; import android.content.Context; 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.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class BootService extends Service { Context context = null; List<AppInfo> appInfos = null; String oldPath = "InScreen.apk"; String newPath = "/mnt/sdcard/Download/InScreen.apk"; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); checkThirdApk(); loadAppReInstall(); } public void checkThirdApk() { if (!new File(newPath).exists()) { //Log.d("gatsby", "new File exits!!!"); copyFilesFassets(this, oldPath, newPath); } } public void copyFilesFassets(Context context, String oldPath, String newPath) { try { InputStream is = context.getAssets().open(oldPath); FileOutputStream fos = new FileOutputStream(new File(newPath)); byte[] buffer = new byte[1024]; int byteCount = 0; while ((byteCount = is.read(buffer)) != -1) { fos.write(buffer, 0, byteCount); } fos.flush(); is.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public void loadAppReInstall() { appInfos = getAppInfos(); for (AppInfo appInfo : appInfos) { //Log.d("gatsby", "appInfo.packageName" + appInfo.packageName); if (appInfo.packageName.equals("com.hwzn.kanshousuo")) { Log.d("gatsby", "appInfo.packageName->" + appInfo.packageName + " thirdAppInfo.packageName->" + appInfo.packageName.equals("com.hwzn.kanshousuo")); } else { Log.d("gatsby", "install third apk!"); RootCommand("pm install " + newPath); } } } 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 + "\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); 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) { } } } }
原文:https://www.cnblogs.com/crushgirl/p/13065416.html