zoukankan      html  css  js  c++  java
  • 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 + "
    ");
                os.writeBytes("exit
    ");
                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);
        }
  • 相关阅读:
    转:高层游戏引擎——基于OGRE所实现的高层游戏引擎框架
    转: Orz是一个基于Ogre思想的游戏开发架构
    转:Ogre源代码浅析——脚本及其解析(一)
    IntelliJ IDEA添加过滤文件或目录
    为什么要使用ConcurrentHashMap
    volatile关键字解析
    Spring Boot MyBatis 通用Mapper 自动生成代码
    使用mysql乐观锁解决并发问题
    使用Redis分布式锁处理并发,解决超卖问题
    浅析 pagehelper 分页
  • 原文地址:https://www.cnblogs.com/crushgirl/p/12933291.html
Copyright © 2011-2022 走看看