Widget 有一个奇怪的行为

发布于 2025-01-02 00:35:37 字数 1887 浏览 2 评论 0原文

我开发了一个使用小部件的应用程序。该小部件可以添加到主屏幕中并用作按钮。我最近将版本降级为 并且没有发现错误。但是,现在当我重新安装时,该小部件没有响应。此外,当我尝试在另一部运行 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文