2012-07-29 181 views
4

如何在appwidget中接收Intent.ACTION_PACKAGE_ADDEDIntent.ACTION_PACKAGE_REMOVED如何接收意圖。 ACTION_PACKAGE_ADDED意圖。 ACTION_PACKAGE_REMOVED在appwidget中?

我一直在努力,在清單中添加的意圖過濾器:

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <!-- The widget provider --> 
     <receiver android:name=".NetsWidgetProvider"> 
      <intent-filter> 
       <action android:name="android.intent.action.PACKAGE_REMOVED"/> 
       <action android:name="android.intent.action.PACKAGE_ADDED"/> 
       <action android:name="com.oozic.widget.incstage.nets.ACTION_NOT_INSTALL"/> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 

      </intent-filter> 
      <!-- This specifies the widget provider info --> 
      <meta-data android:name="android.appwidget.provider" 
        android:resource="@xml/widgetinfo" /> 
     </receiver> 

    </application> 

,我也試過寄存器代碼:

@Override 
    public void onEnabled(Context context) { 

     registerReceiver(context); 
     Utils.log(TAG, "Register PACKAGE_ADDED PACKAGE_REMOVED"); 

    } 

    private void registerReceiver(Context context) { 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_PACKAGE_ADDED); 
     filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 
     context.getApplicationContext().registerReceiver(this, filter); 

    } 

但兩者沒有工作。 謝謝!

+0

當您添加或刪除自己的包時,您是否試圖接聽電話?因爲這不起作用:http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED – 2012-07-29 12:23:32

+0

嗨,時間,什麼是「請注意,新安裝的軟件包不會收到此廣播。 「?多久可以算作「新」? – herbertD 2012-07-30 01:41:45

+0

對不起,這個不清楚的定義。對於新安裝的軟件包,我的意思是包含此代碼的軟件包。如果您使用此代碼安裝您的軟件包,則不會在您將其刪除時收到通知。 – 2012-07-30 10:24:15

回答

1

當我收到PACKAGE_REMOVED/ADDED消息時,我會在自定義啓動器中向我的小部件發送廣播。這是我發現解決這個問題的唯一工作。

+0

其實你可以在清單中使用意圖。你只需要處理onReceive而不是onUpdate。 – 2013-04-13 21:27:31

3
<receiver android:name=".NetsWidgetProvider" 
     android:label="@string/appwidget_name" android:icon="@drawable/icon"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      <action android:name="com.oozic.widget.incstage.nets.ACTION_NOT_INSTALL"/> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.PACKAGE_DATA_CLEARED"/> 
      <action android:name="android.intent.action.PACKAGE_REMOVED"/> 
      <data android:scheme="package" /> 
     </intent-filter> 
     <!-- This specifies the widget provider info --> 
     <meta-data android:name="android.appwidget.provider" 
       android:resource="@xml/appwidgetinfo" /> 
    </receiver> 
+4

作爲一個補充,這裏的關鍵是在OP的原始清單中缺少的「android:scheme」。在Java中,您可以像這樣設置方案: filter.addDataScheme(「package」); – 2013-08-30 15:29:32

8

在AndroidManifest.xml中加入<data android:scheme="package" />解決了這個問題。

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