• Android APK安装完成自动删除安装包


     

    需要实现此功能,一般实际开发是在自动版本更新上,当更新完开始自动安装完毕后,删除内存卡里的安装包。实现方式很简单,监听应用广播,获取内存卡下的文件,删除!
    1、监听广播
    [java] view plain copy
    1. package com.example.a75213.testdownloaddemo;  
    2.   
    3. import android.content.BroadcastReceiver;  
    4. import android.content.Context;  
    5. import android.content.Intent;  
    6. import android.widget.Toast;  
    7.   
    8. import com.example.a75213.testdownloaddemo.contant.comm;  
    9.   
    10. /** 
    11.  * Created by 75213 on 2017/11/7. 
    12.  */  
    13.   
    14. public class InitApkBroadCastReceiver extends BroadcastReceiver {  
    15.     @Override  
    16.     public void onReceive(Context context, Intent intent) {  
    17.   
    18.         if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {  
    19.             comm.rmoveFile("http://imtt.dd.qq.com/16891/7C7BB50B68B684A36339AF1F615E2848.apk");  
    20.             Toast.makeText(context , "监听到系统广播添加" , Toast.LENGTH_LONG).show();  
    21.         }  
    22.   
    23.         if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {  
    24.             comm.rmoveFile("http://imtt.dd.qq.com/16891/7C7BB50B68B684A36339AF1F615E2848.apk");  
    25.             Toast.makeText(context , "监听到系统广播移除" , Toast.LENGTH_LONG).show();  
    26.             System.out.println("");  
    27.         }  
    28.   
    29.         if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {  
    30.             comm.rmoveFile("http://imtt.dd.qq.com/16891/7C7BB50B68B684A36339AF1F615E2848.apk");  
    31.             Toast.makeText(context , "监听到系统广播替换" , Toast.LENGTH_LONG).show();  
    32.         }  
    33.     }  
    34. }  
    2、记得给删除权限和广播注册【AndroidMainifest】
    [html] view plain copy
    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    2. <receiver  
    3.             android:name=".InitApkBroadCastReceiver"  
    4.             android:enabled="true">  
    5.             <intent-filter>  
    6.                 <action android:name="android.intent.action.PACKAGE_ADDED" />  
    7.                 <action android:name="android.intent.action.PACKAGE_REPLACED" />  
    8.                 <action android:name="android.intent.action.PACKAGE_REMOVED" />  
    9.   
    10.                 <data android:scheme="package" />  
    11.             </intent-filter>  
    12.         </receiver>  
     
    3、删除工具类
    [html] view plain copy
    1. package com.example.a75213.testdownloaddemo.contant;  
    2.   
    3. import android.os.Environment;  
    4.   
    5. import java.io.File;  
    6.   
    7. /**  
    8.  * Created by 75213 on 2017/11/1.  
    9.  */  
    10.   
    11. public class comm{  
    12.     public static File getPathFile(String path){  
    13.         String apkName = path.substring(path.lastIndexOf("/"));  
    14.         File outputFile = new File(Environment.getExternalStoragePublicDirectory  
    15.                 (Environment.DIRECTORY_DOWNLOADS), apkName);  
    16.         return outputFile;  
    17.     }  
    18.   
    19.     public static void rmoveFile(String path){  
    20.         File file = getPathFile(path);  
    21.         file.delete();  
    22.     }  
    23. }  
  • 相关阅读:
    Bozh 的技术博客 梦想成为Gnu/Linux | Unix后台架构师 | Read the fucking source code
    Welcome to the TANGO website
    repowatcher : about
    PyTango documentation — PyTango 8.0.2 documentation
    Visual Studio 2010旗舰版正式版序列号 civilman的专栏 博客频道 CSDN.NET
    BoostPro Binary Installer for Visual C++
    分享:httping 1.5.6 发布,HTTP 诊断工具
    浅析epoll – epoll函数深入讲解 C++爱好者博客
    分享:Jython动态加载Jar
    News BoostPro Computing BoostPro
  • 原文地址:https://www.cnblogs.com/xgjblog/p/9232918.html
Copyright © 2020-2023  润新知