Android apk安裝後自動添加桌面圖標 和 自啓動的嘗試

AndroidSchemeXML android

前一個問題是論壇裏的一位朋友提出來的:「如何在應用安裝到手機裏時,自動在桌面增長快捷方式?」 ,第二個問題是在網上看到的:「apk安裝後如何自啓動」 。app


很顯然,除非在應用安裝後有相關的廣播能被捕獲到,不然就無法作了,事實是有的:Intent.ACTION_PACKAGE_ADDED。ide

Launcher中的應用列表正是這麼作的:this

<Launcher.java>
spa

Java代碼  收藏代碼orm

  1. /** xml

  2.      * Registers various intent receivers. The current implementation registers blog

  3.      * only a wallpaper intent receiver to let other applications change the 事件

  4.      * wallpaper. 

  5.      */  

  6.     private void registerIntentReceivers() {  

  7.         IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);  

  8.         filter.addAction(Intent.ACTION_PACKjAGE_REMOVED);  

  9.         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);  

  10.         filter.addDataScheme("package");  

  11.         registerReceiver(mApplicationsReceiver, filter);  

  12.         filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  

  13.         registerReceiver(mCloseSystemDialogsReceiver, filter);  

  14.     }  

 

我依樣畫葫蘆嘗試了一下,直接在個人應用中實現BroadcastReceiver並在XML註冊,對action=Intent.ACTION_PACKAGE_ADDED進行捕獲,可是沒捕獲到。

後來一查文檔才發現這是行不通的:

public static final  String   ACTION_PACKAGE_ADDED

Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does  not  receive this broadcast.

看來在應用自身中經過BroadcastReceiver來捕獲Add消息是不行的,可是我想到了另外一種折中的實現方法——經過另外一個應用來輔助實現。

須要先實現一個包含對ntent.ACTION_PACKAGE_ADDED進行捕獲的BroadcastReceiver的應用,首先安裝到手機上,在他接受到消息後再向你的應用返回一個廣播。你須要在你的應用中實現實現對應的BroadcastReceiver。

具體實現:

<輔助apk>

Java代碼  收藏代碼

  1. public class PackageChangeReceiver extends BroadcastReceiver {  

  2.   

  3.       @Override  

  4.       public void onReceive(Context context, Intent intent) {  

  5.             final String action = intent.getAction();  

  6.         final String packageName = intent.getData().getSchemeSpecificPart();  

  7.         final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);  

  8.         // 通知對應的應用  

  9.         Intent notifyIntent = new Intent("com.app.action.notifier");  

  10.         notifyIntent.setPackage(packageName);  

  11.         notifyIntent.putExtra("action", action);  

  12.         notifyIntent.putExtra("replace", replacing);  

  13.             context.sendBroadcast(notifyIntent);  

  14.       }  

  15.   

  16. }  

 

Java代碼  收藏代碼

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  

  3.       package="com.app.notifier"  

  4.       android:versionCode="1"  

  5.       android:versionName="1.0">  

  6.     <uses-sdk android:minSdkVersion="7" />  

  7.   

  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  

  9.         <receiver android:name="PackageChangeReceiver">  

  10.             <intent-filter>  

  11.                 <action android:name="android.intent.action.PACKAGE_ADDED"></action>  

  12.                 <action android:name="android.intent.action.PACKAGE_CHANGED"></action>  

  13.                 <action android:name="android.intent.action.PACKAGE_REMOVED"></action>  

  14.                 <data android:scheme="package"></data>  

  15.             </intent-filter>  

  16.         </receiver>  

  17.   

  18.     </application>  

  19. </manifest>  

 

<你的應用> 

Java代碼  收藏代碼

  1. public class PackageChangeReceiver extends BroadcastReceiver {  

  2.         

  3.       @Override  

  4.       public void onReceive(Context context, Intent intent) {  

  5.             final String action = intent.getStringExtra("action");  

  6.             boolean replace = intent.getBooleanExtra("replace"false);  

  7.             if(action.equals(Intent.ACTION_PACKAGE_ADDED)){  

  8.                  // do some thing you want.  

  9.             }  

  10.       }  

  11. }  

xml中註冊廣播 

Java代碼  收藏代碼

  1. <receiver android:name="PackageChangeReceiver">  

  2.      <intent-filter>  

  3.          <action android:name="com.app.action.notifier"></action>  

  4.      </intent-filter>  

  5. </receiver>  

 

優缺點 缺點:必須先安裝輔助apk優勢:僅需一次安裝,以後使用只需在應用中實現並在XML中註冊BroadcastReceiver就能捕獲到安裝事件,從而執行相應的操做(添加桌面圖標,自啓動...)

相關文章
相關標籤/搜索