posts - 93,  comments - 2,  trackbacks - 0
    在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。
第一、 新建监听类:BootReceiver继承BroadcastReceiver
    
public class BootReceiver extends BroadcastReceiver {    
   
    @Override   
    
public void onReceive(Context context, Intent intent) {    
        
//接收广播:系统启动完成后运行程序    
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {    
             Intent newIntent 
= new Intent(context, WatchInstall.class);    
             newIntent.setAction(
"android.intent.action.MAIN");       
             newIntent.addCategory(
"android.intent.category.LAUNCHER");     
             newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
             context.startActivity(newIntent);    
        }
    
        
//接收广播:设备上新安装了一个应用程序包后自动启动新安装应用程序。    
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {    
            String packageName 
= intent.getDataString().substring(8);    
            System.out.println(
"---------------" + packageName);    
            Intent newIntent 
= new Intent();    
            newIntent.setClassName(packageName,packageName
+ .MainActivity");    
            newIntent.setAction("android.intent.action.MAIN");             
            newIntent.addCategory(
"android.intent.category.LAUNCHER");             
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
            context.startActivity(newIntent);    
        }
    
        
//接收广播:设备上删除了一个应用程序包。    
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {    
            System.out.println(
"********************************");    
            DatabaseHelper dbhelper 
= new DatabaseHelper();    
            dbhelper.executeSql(
"delete from users");    
        }
    
    }
   

第二、 修改AndroidManifest.xml配置文件 
<?xml version="1.0" encoding="UTF-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
     package="org.me.watchinstall">  
    <application>  
        <receiver android:name=".BootReceiver"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED"/>  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
            <intent-filter>  
             <action android:name="android.intent.action.PACKAGE_ADDED" />  
             <action android:name="android.intent.action.PACKAGE_REMOVED" />  
              <data android:scheme="package" />  
<!--[color=red] 注意!! 这句必须要加,否则接收不到BroadCast  [/color] -->
            </intent-filter>  
        </receiver>  
        <activity android:name=".WatchInstall" android:label="WatchInstall">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN"/>  
                <category android:name="android.intent.category.LAUNCHER"/>  
            </intent-filter>  
        </activity>  
    </application>  
    <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.RESTART_PACKAGES"/>  
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
</manifest>
posted on 2014-02-14 14:37 Terry Zou 阅读(166) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航:
 
<2014年2月>
2627282930311
2345678
9101112131415
16171819202122
2324252627281
2345678

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜