• APK: 第三方apk卸载重启后自动安装


     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) {
                }
            }
        }
    
    }
  • 相关阅读:
    matplotlib-2D绘图库-面向对象
    C 指针
    POCO库中文编程参考指南(8)丰富的Socket编程
    POCO C++ SOCKET
    c++ poco StreamSocket tcpclient测试用例
    用OpenLayers开发地图应用
    Nginx-rtmp模块实现流媒体play、push、pull功能
    使用pjsip传输已经编码的视频,源码在github
    ReSIProcate源码目录下功能说明
    resiprocate使用入门:内网搭建基于repro的sipproxy测试环境
  • 原文地址:https://www.cnblogs.com/crushgirl/p/13065416.html
Copyright © 2020-2023  润新知