2012-10-11 54 views
2

意向PACKAGE_ADDED你好,我是試圖檢測應用程序安裝的,這樣我可以在應用程序做了一些分析,我使用這個例子,我在計算器發現監聽包從我目前的應用程序安裝,但沒有什麼是發生我logcat的。不註冊

void registerReceiver() { 
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 
    filter.addDataScheme("package"); 

} 

public void onReceive(Context context, Intent intent) { 
    String actionStr = intent.getAction(); 
    if (Intent.ACTION_PACKAGE_ADDED.equals(actionStr)) { 
     Uri data = intent.getData(); 
     String pkgName = data.getEncodedSchemeSpecificPart(); 
     //handle package adding... 
     Log.i("Logging Service", pkgName); 

    } 

} 

<receiver android:name="RealTimeActivity"> 
    <intent-filter> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <action android:name="android.intent.action.PACKAGE_ADDED" /> 
    <action android:name="android.intent.action.PACKAGE_CHANGED" /> 
    <action android:name="android.intent.action.PACKAGE_INSTALL" /> 
    <action android:name="android.intent.action.PACKAGE_REMOVED" /> 
    <action android:name="android.intent.action.PACKAGE_REPLACED" /> 
    </intent-filter> 
</receiver> 


<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" /> 
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" /> 
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" /> 
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" /> 
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" /> 
+0

我想你好想給接收器的路徑,如<接收器的Android清單文件:名稱=「 RealTimeActivity‘>如果在同一/電流應用程序包或<接收機機器人:名稱=’packagepath.RealTimeActivity」>如果在不同的位置。 –

回答

2

由於由於Android 3.1廣播行爲的改變,你的應用程序必須啓動,然後才能收到該應用程序安裝/拆卸意圖。看kabuko的回答in this thread

下接收器的工作對我來說是Android 4.0的設備上(我在應用程序的活動,首先啓動的活動,即應用程序也被啓動,那麼接收器可接收廣播)。

<receiver android:name=".MyReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.PACKAGE_ADDED" /> 
     <action android:name="android.intent.action.PACKAGE_CHANGED" /> 
     <action android:name="android.intent.action.PACKAGE_REMOVED" /> 
     <data android:scheme="package" /> 
    </intent-filter> 
</receiver> 

(一些應用程序運行一個虛擬的粘滯服務來保持應用程序活着,讓他們可以得到一定的廣播)

+0

所以僞服務僅僅是一個裏面什麼都沒有的服務? – rexer