具有多个按钮的 android 小部件

Posted

技术标签:

【中文标题】具有多个按钮的 android 小部件【英文标题】:android widget with multiple buttons 【发布时间】:2012-04-21 18:57:02 【问题描述】:

我开发了一个带有多个按钮的 android 小部件。它工作正常,我第一次添加。但是当我重新启动设备按钮时不会触发。这就是我所做的

这里是xml配置

<receiver android:name="com.islem.widget.MyWidgetMain" android:label="@string/app_name">
<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <!-- Broadcast Receiver that will also process our self created action -->
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER1"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER2"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER3"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER4"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER5"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER6"/>
    <action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RELOAD_ALARM"/>

</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/k_widget_provider" />

这里是 onUpdate 方法

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)       

    for (int appWidgetId : appWidgetIds) 

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.kalan_widget_main);

        Intent refreshButtonIntent = new Intent(context, KalanWidgetMain.class);
        refreshButtonIntent.setAction(ACTION_WIDGET_RECEIVER1);

        Intent voiceTabIntent = new Intent(context, KalanWidgetMain.class);
        voiceTabIntent.setAction(ACTION_WIDGET_RECEIVER2);

        Intent dataTabIntent = new Intent(context, KalanWidgetMain.class);
        dataTabIntent.setAction(ACTION_WIDGET_RECEIVER3);

        Intent smsTabIntent = new Intent(context, KalanWidgetMain.class);
        smsTabIntent.setAction(ACTION_WIDGET_RECEIVER4);

        Intent prevButtonIntent = new Intent(context, KalanWidgetMain.class);
        prevButtonIntent.setAction(ACTION_WIDGET_RECEIVER5);

        Intent nextButtonIntent = new Intent(context, KalanWidgetMain.class);
        nextButtonIntent.setAction(ACTION_WIDGET_RECEIVER6);

        refreshButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        voiceTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        dataTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        smsTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        prevButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        nextButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        PendingIntent refreshButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, refreshButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent voiceTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, voiceTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent dataTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, dataTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent smsTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, smsTabIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent prevButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, prevButtonIntent, 0);
        PendingIntent nextButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, nextButtonIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.kalanRefresh, refreshButtonPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabVoiceBg, voiceTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabDataBg, dataTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanTabSMSBg, smsTabPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanPrev, prevButtonPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.kalanNext, nextButtonPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        KalanUpdater.registerWidget(context, appWidgetManager, appWidgetId); 
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    

这里是 onReceive 方法

    @Override
public void onReceive(Context context, Intent intent) 

    System.out.println("on onReceive");     
    Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();

    // v1.5 fix that doesn't call onDelete Action
    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(context, new int[]  appWidgetId );
        
     else 

        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER1) || intent.getAction().equals(ACTION_WIDGET_RECEIVER2) ||
                intent.getAction().equals(ACTION_WIDGET_RECEIVER3) || intent.getAction().equals(ACTION_WIDGET_RECEIVER4) ||
                intent.getAction().equals(ACTION_WIDGET_RECEIVER5) || intent.getAction().equals(ACTION_WIDGET_RECEIVER6) ||
                intent.getAction().equals(ACTION_WIDGET_RELOAD_ALARM) 
                )              
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            System.out.println("appWidgetId:" + appWidgetId + ", action:" + action);




        


        super.onReceive(context, intent);
    

【问题讨论】:

你有没有把你的小部件上传到我需要查看它的代码的地方。如果你能帮上忙,那真的很有帮助。我是 Android 新手,正在尝试构建一个小部件。 【参考方案1】:

手机重启时会调用onEnable而不是onUpdate

--更新--

int[] allids = AppWidgetManager
.getInstance(Context)
.getAppWidgetIds(new ComponentName(Context, YourAppWidgetProvider.class);

这将获取受 YourAppWigetProvider 控制的所有 id。阅读更多关于this

【讨论】:

我已将 onupdate 的内容移动到一个方法中,并从 onEnabled 和 onUpdate 调用,如下所示,但不起作用。我也可以在重新启动时看到 onUpdate 日志。 @Override public void onEnabled(Context context) // TODO 自动生成的方法存根 super.onEnabled(context); Toast.makeText(context, "onEnabled", Toast.LENGTH_SHORT).show(); int[] appWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, KalanWidgetMain.class)); startWidget(context, AppWidgetManager.getInstance(context), appWidgetIds); 不要从 onUpdate 中删除,只在 onEnabled 中复制

以上是关于具有多个按钮的 android 小部件的主要内容,如果未能解决你的问题,请参考以下文章

通过 QStackedWidget 动态修改同一个小部件的多个实例

在 android studio 中创建自定义小部件?

带有下拉列表的android按钮小部件,这个特定小部件的名称是啥?

在flutter中使用listview.builder添加多个彼此不同的动态小部件

为Android中的小部件启用小部件设置按钮?

android小部件的按钮单击事件