Widget有一种奇怪的行为

时间:2012-02-01 13:23:36

标签: android android-widget

我开发了一个使用小部件的应用程序。小部件可以添加到主屏幕中并用作按钮。我最近将版本降级为<uses-sdk android:minSdkVersion="7" />,没有发现任何错误。但是,现在当我重新安装时,窗口小部件没有响应。此外,当我尝试在运行android 4.0.3的另一部手机上安装该应用程序时,小部件在任何地方都无法使用,所以我甚至无法将其添加到主屏幕。这是我的小部件代码:

public class MyWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context c, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    final int N = appWidgetIds.length;
             //for each of the instances of the widget
     for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
    Intent fireDialog = new Intent(c,Execute.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, fireDialog, 0);
    views.setOnClickPendingIntent(R.id.myButton, pendingIntent); //when the button is clicked, an activity is launched

    appWidgetManager.updateAppWidget(appWidgetId, views);
     }
}

@Override
public void onReceive(Context c, Intent intent) 
{
 final String action=intent.getAction();
 if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
     final int appWidgetId=intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
     if(appWidgetId!=AppWidgetManager.INVALID_APPWIDGET_ID){
         this.onDeleted(c,new int[]{appWidgetId});
     }
 }else{
     super.onReceive(c, intent);
 } 
}
}

这是manifest.xml:

<application>

<receiver android:name=".myButton" 
  android:icon="@drawable/icon"
   android:label="myWidget">
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
           android:resource="@xml/my_xml" />
</receiver>

</application>

0 个答案:

没有答案