发送广播 更新apk

一、功能实现

a、发收广播

b、AS隐藏apk应用图标

二、代码

1.1 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xinhua.updateapk">

    <!--开机广播 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <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>

        <service android:name=".UpateService"></service>
        
        <receiver android:name=".UpdateBroadcast">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>

</manifest> 

1.2 MainActivity.java

package com.xinhua.updateapk;

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 startService = new Intent(this, UpateService.class);
        //startService(startService);

    }
    
} 

1.2 UpdateBroadcast.java

package com.xinhua.updateapk;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class UpdateBroadcast extends BroadcastReceiver {

    Context mContext;

    private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        this.mContext = context;

        String action = intent.getAction();

        if (action.equals(ACTION_BOOT)) {
            Log.d("gatsby", "UpdateService!");
            Intent startService = new Intent(context, UpateService.class);
            context.startService(startService);
        }

    }

}

1.3 UpdateService.java 

package com.xinhua.updateapk;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

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

public class UpateService extends Service {

    private final String UPDATE_APK = "com.xinhua.updateApk";
    private String APK_PATH = "/mnt/internal_sd/update/*";
    UpdateReceiver updateReceiver = new UpdateReceiver();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("gatsby", "UpdateService is Start!");

        IntentFilter filter = new IntentFilter();
        filter.addAction(UPDATE_APK);
        registerReceiver(updateReceiver, filter);
    }

    public class UpdateReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(UPDATE_APK)) {
                String packageName = intent.getStringExtra("PACKAGE_NAME");

                //Log.d("gatsby", "receiver intent Action -> " + intent.getAction());
                
                RootCommand("pm uninstall " + packageName);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                RootCommand("pm install " + APK_PATH);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String className = getLauncherActivityNameByPackageName(packageName);
                //Log.d("gatsby", "className -> " + className);
                RootCommand("am start " + packageName + "/" + className);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(updateReceiver);
    }

    //根据包名 得出类名
    public String getLauncherActivityNameByPackageName(String packageName) {
        String lunchClassName = null;
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageName);
        List<ResolveInfo> resolveinfoList = getPackageManager().queryIntentActivities(resolveIntent, 0);
        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            lunchClassName = resolveinfo.activityInfo.name;
        }
        return lunchClassName;
    }


    private void 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);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
    }


}

2.1  发送广播

    public void sendbBroast() {
        Intent intent = new Intent();
        intent.setAction("com.xinhua.updateApk");
        intent.putExtra("PACKAGE_NAME", "com.gatsby.zebra");
        sendBroadcast(intent);
    }

猜你喜欢

转载自www.cnblogs.com/crushgirl/p/12933291.html