posts - 93,  comments - 2,  trackbacks - 0
              在android系統(tǒng)中,安裝和卸載都會(huì)發(fā)送廣播,當(dāng)應(yīng)用安裝完成后系統(tǒng)會(huì)發(fā)android.intent.action.PACKAGE_ADDED廣播。可以通過(guò)intent.getDataString()獲得所安裝的包名。當(dāng)卸載程序時(shí)系統(tǒng)發(fā)android.intent.action.PACKAGE_REMOVED廣播。同樣intent.getDataString()獲得所卸載的包名。
          第一、 新建監(jiān)聽(tīng)類(lèi):BootReceiver繼承BroadcastReceiver
              
          public class BootReceiver extends BroadcastReceiver {    
             
              @Override   
              
          public void onReceive(Context context, Intent intent) {    
                  
          //接收廣播:系統(tǒng)啟動(dòng)完成后運(yùn)行程序    
                  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);    
                  }
              
                  
          //接收廣播:設(shè)備上新安裝了一個(gè)應(yīng)用程序包后自動(dòng)啟動(dòng)新安裝應(yīng)用程序。    
                  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);    
                  }
              
                  
          //接收廣播:設(shè)備上刪除了一個(gè)應(yīng)用程序包。    
                  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)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          相冊(cè)

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 子长县| 东光县| 阳曲县| 高要市| 道孚县| 汕头市| 奉新县| 顺平县| 建德市| 扎兰屯市| 普安县| 梅河口市| 从江县| 天峻县| 镇宁| 枣强县| 普洱| 深水埗区| 内丘县| 英吉沙县| 改则县| 吉安县| 会理县| 永川市| 永城市| 双江| 绵竹市| 绥棱县| 巴南区| 绥滨县| 乐平市| 都兰县| 双牌县| 高尔夫| 黄陵县| 青海省| 盐山县| 娄烦县| 霍州市| 赞皇县| 巢湖市|