如何将数据从应用程序发送到AppWidgetProvider? [英] How send data from application to AppWidgetProvider?

查看:112
本文介绍了如何将数据从应用程序发送到AppWidgetProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在特定的情况下.用户从应用程序更新时间后,我需要立即更新我的小部件.我确实尝试过通过Intent Extras发送数据来进行广播,但是没有这样做.目前,我的数据存储在 AppWidgetProvider 中,我需要将此数据发送到服务

I am stuck in a particular scenario. I need to get my widget updated as soon as the user update the time from the app. I did try Broadcast by sending the data through Intent Extras but fail to do so. Currently, I have my data in AppWidgetProvider and I need to send this data to service

public class CountdownWidget extends AppWidgetProvider {
    // SharedPreferences userDefaults;

    // update rate in milliseconds
    public static final int UPDATE_RATE = 1800000; // 30 minute

    public static String nameOne = "";

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            setAlarm(context, appWidgetId, -1);
        }
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle extras = intent.getExtras();
        nameOne = extras.getString("NAME_ONE");

        Log.e("Name: ", nameOne);

        super.onReceive(context, intent);
    }

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

        for (int appWidgetId : appWidgetIds) {
            setAlarm(context, appWidgetId, UPDATE_RATE);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    public static void setAlarm(Context context, int appWidgetId, int updateRate) {

        // Log.e("", "Updating Widget Service");

        PendingIntent newPending = makeControlPendingIntent(context,
                CountdownService.UPDATE, appWidgetId);
        AlarmManager alarms = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        if (updateRate >= 0) {
            alarms.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime(), updateRate, newPending);
        } else {
            // on a negative updateRate stop the refreshing
            alarms.cancel(newPending);
        }
    }

    public static PendingIntent makeControlPendingIntent(Context context,
                                                         String command, int appWidgetId) {
        Intent active = new Intent(context, CountdownService.class);
        active.setAction(command);
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        // this Uri data is to make the PendingIntent unique, so it wont be
        // updated by FLAG_UPDATE_CURRENT
        // so if there are multiple widget instances they wont override each
        // other
        Uri data = Uri.withAppendedPath(
                Uri.parse("countdownwidget://widget/id/#" + command
                        + appWidgetId), String.valueOf(appWidgetId));
        active.setData(data);
        return (PendingIntent.getService(context, 0, active,
                PendingIntent.FLAG_UPDATE_CURRENT));
    }
}

您会看到 nameOne 是一个静态变量.我可以使用getExtras通过 onReceive 方法接收数据,但是我无法将这些数据传递给我的 CountdownService 服务.

As you see nameOne is a static variable. I can receive data on the onReceive method using getExtras, but I am not unable to pass this data to my CountdownService Service.

我确实尝试过 CountdownWidget.nameOne ,但仍然无法在服务中找到数据.

I did try CountdownWidget.nameOne but still fails to fet the data in the service.

任何帮助将不胜感激.

推荐答案

您必须创建一个更新小部件的服务,请检查

You have to create a service that updates the widget,check this tutorial (the 4th section), you may try to transfer the data through static variables.

这篇关于如何将数据从应用程序发送到AppWidgetProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆