Widget has a weird bahaviour

橙三吉。 提交于 2020-01-03 02:52:33

问题


I have developed an application that makes use of a widget. The widget could be added in the homescreen and used as a button. I have recently downgraded the version to <uses-sdk android:minSdkVersion="7" /> and no errors were spotted. However, now when i reinstall the, the widget is not responsive. In addition when I try to install the app on another phone running android 4.0.3, widget is not available anywhere, so i cannot even add it to the homescreen. Here is my code for the widget:

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);
 } 
}
}

Here is the 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>

来源:https://stackoverflow.com/questions/9096518/widget-has-a-weird-bahaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!