现在的位置: 首页 > 综合 > 正文

Android应用程序升级后自动进入新版本

2017年12月17日 ⁄ 综合 ⁄ 共 2778字 ⁄ 字号 评论关闭

在新版本中创建一个Receiver接收手机应用的安装和卸载(可以监听到旧版本的卸载)

  1. package  
  2. com.justsy.lpi.receiver;  
  3.   
  4. import  
  5. android.content.BroadcastReceiver;  
  6. import  
  7. android.content.Context;  
  8. import  
  9. android.content.Intent;  
  10.   
  11. public  
  12. class PkInstallReceiver extends BroadcastReceiver {  
  13.   
  14.        
  15. @Override  
  16.        
  17. public void onReceive(Context context, Intent intent) {  
  18.   
  19.              
  20. if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED" )) {    
  21.             String packageName = intent.getDataString().substring(8);  
  22.             System.  
  23. out.println( "安装:" +packageName + "包名的程序" );  
  24.         }    
  25.          
  26. //接收卸载广播    
  27.          
  28. if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED" )) {    
  29.             String packageName = intent.getDataString().substring(8);    
  30.             System.  
  31. out.println( "卸载:"  + packageName + "包名的程序" );  
  32.             Intent newIntent =  
  33. new Intent();  
  34.             newIntent.setClassName(packageName,packageName+  
  35. ".AutoStartProTestActivity" );     
  36.             newIntent.setAction(  
  37. "android.intent.action.MAIN");                 
  38.             newIntent.addCategory(  
  39. "android.intent.category.LAUNCHER" );                 
  40.             newIntent.setFlags(Intent.  
  41. FLAG_ACTIVITY_NEW_TASK);        
  42.             context.startActivity(newIntent);  
  43.         }  
  44.      }  
  45.   
  46. }  
AndroidManifest.xml进行配置
  1.    <receiver android:name="com.justsy.lpi.receiver.PkInstallReceiver"    
  2.             android:label=" @string/app_name">     
  3.       <intent-filter>    
  4.        <action android:name="android.intent.action.PACKAGE_ADDED" />   
  5.        <action android:name="android.intent.action.PACKAGE_REMOVED" />   
  6.         <data android:scheme="package" />   
  7.       </intent-filter>    
  8.   </receiver>  
  9.   
  10. <uses-permission android:name"android.permission.RESTART_PACKAGES" />   
  11. t;uses-permission android:name"android.permission.RECEIVE_BOOT_COMPLETED" />

抱歉!评论已关闭.