从应用程序发送数据到窗口小部件

问题描述:

我被困在一个特定之情况。我需要让我的小部件,尽快更新用户更新从应用程序的时间。我也尝试通过发送thorugh意图Extras中的数据广播,但不这样做。目前,我有我的AppWidgetProvider数据,我需要这个数据发送到服务

I am stuck in a particular scenerio. 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 thorugh Intent Extras but fail to do so. Currently, I have my data in AppWidgetProvider and I need to send this data to service

公共类CountdownWidget扩展AppWidgetProvider {
    //共享preferences userDefaults;

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

}

正如你所看到的名称一是一个静态变量。我可以在的onReceive 使用方法getExtras接收数据,但我不是无法这一数据传递给我的 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 ,但仍然未能FET在服务中的数据。

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

任何帮助将是非常美联社preciated。

Any help will be highly appreciated.

您必须创建一个更新微件服务,检查的本教程(第四部分),您可以尝试通过静态变量来传输数据。

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.