【问题标题】:Android home screen widgetAndroid 主屏幕小部件
【发布时间】:2017-03-03 10:30:58
【问题描述】:

我是第一次进行小部件开发。所以请多多包涵。我有一个从onStart() 启动小部件的Android 服务。所以我在我的WidgetProvideronUpdate() 中并更新按钮之类的视图。 现在我的问题是 -

1) 我如何处理小部件中的按钮按下并告诉服务按钮按下以便服务正常工作?

2) 服务完成工作后,需要更新小部件按钮,我该如何处理?

3) 当应用被杀死时,服务也被杀死。那么当我点击小部件时,如何确定该应用被杀死并打开应用?

4) 如果应用中的 UI 正在与服务通信并接收服务的响应,我如何将这些响应也告诉小部件?

我的小部件代码是这样的 -

public class MyWidgetProvider extends AppWidgetProvider {

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

        RemoteViews remoteViews;
        ComponentName componentName;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        componentName = new ComponentName(context, MyWidgetProvider.class);

        remoteViews.setOnClickPendingIntent(R.id.button1,getPendingSelfIntent(context, "Play",componentName));
        remoteViews.setOnClickPendingIntent(R.id.button2,getPendingSelfIntent(context, "Pause",componentName));

        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);

        if ("Play".equals(intent.getAction())) {

        //I really dont know what to do here....

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, MyWidgetProvider.class);



          appWidgetManager.updateAppWidget(watchWidget, remoteViews);

        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
        Intent intent = new Intent(context, MyService.class);
        intent.setAction(action);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
        return PendingIntent.getService(context, 0, intent, 0);
    }
}

请求你在这方面帮助我

【问题讨论】:

    标签: java android widget android-widget


    【解决方案1】:

    1) 你已经用你的getPendingSelfIntent() 方法做到了。虽然你的目标应该是 MyWidgetProvider 像这样:

    Intent intent = new Intent(context, MyWidgetProvider.class);
    

    然后在您的onReceive 中,您将检查意图操作是否为“播放”并从那里启动您的服务:

    Intent serviceIntent = new Intent(context, MyService.class);
    

    2) 为了让您的服务与您的 Widget 进行通信,您需要发送一个广播:

    Intent widgetUpdateIntent = new Intent(this, MyWidgetProvider.class);
    widgetUpdateIntent.setAction(ApplicationEvents.DATA_FETCHED);//this is your custom action
    widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    sendBroadcast(widgetUpdateIntent);
    

    3) 您不需要显式打开应用程序(实际上您不能这样做),如果您将更新周期设置为 30 分钟,您将收到 onUpdate 回调并且您需要启动您的那里也有服务。

    4) 当操作与您的自定义操作 (ApplicationEvents.DATA_FETCHED) 匹配时,您将在 onReceive 事件中更新 UI。

    【讨论】:

    • 感谢您的回答 :) 在第三点,我实际上在 xml 文件中有我的 android:updatePeriodMillis="1000" 。但我见过 Spotify,其中当应用程序被终止并且您单击小部件时,它们会明确打开应用程序。
    • 请注意,最小值为 30 分钟,如 documentation 中所述。为了使用较低的值,您需要使用 AlarmManager。关于 Spotify,当您单击小部件时是一个不同的场景,因为小部件提供者将处理意图,因此它与计划的更新无关。
    • 是的,我也需要类似于 Spotify。我不需要预定的更新。我该怎么做?
    • 如果您将属性 android:updatePeriodMillis 设置为零,例如,您将禁用每 30 分钟更新一次。如果您只需要在用户单击按钮时执行某项操作,您只需要使用操作(播放或暂停)设置待处理意图并在 onReceive 事件上处理它们。
    • 哦,这是很棒的信息 :) 所以当应用程序被终止并且服务被终止时,我想将小部件中的按钮变灰。所以用户点击小部件时,必须打开应用程序,应用程序将创建服务。我该怎么做?
    猜你喜欢
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多