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="     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>
          <2014年2月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          2324252627281
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 仪陇县| 陵川县| 腾冲县| 赞皇县| 阿克苏市| 抚州市| 张家口市| 沂南县| 布拖县| 乐亭县| 滦平县| 黑龙江省| 卫辉市| 南丹县| 长沙市| 涪陵区| 上林县| 云龙县| 大英县| 长顺县| 长沙市| 呼伦贝尔市| 平舆县| 曲周县| 绥宁县| 苏尼特左旗| 额尔古纳市| 泽普县| 云梦县| 静海县| 湖口县| 丹寨县| 搜索| 界首市| 凤冈县| 庆阳市| 游戏| 辽阳县| 正镶白旗| 二连浩特市| 望江县|