android - 如何将数据从应用程序发送到 AppWidgetProvider?

标签 android android-widget android-preferences

我被困在一个特定的场景中。我需要在用户从应用程序更新时间后立即更新我的小部件。我确实通过 Intent Extras 发送数据来尝试广播,但没有成功。目前,我的数据在 AppWidgetProvider 中,我需要将这些数据发送到服务

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 服务。

我确实尝试了 CountdownWidget.nameOne 但仍然无法获取服务中的数据。

我们将不胜感激任何帮助。

最佳答案

我通过在我构建的 Intent 中添加额外内容来更新小部件来实现这一点。此外,当小部件具有信息时,它可以再次传递到填充小部件 View 的 RemoteViewsService

在我的 UpdateWidget 帮助器方法中,我传入了 userId(由应用程序中的各种 Activity 调用,这些 Activity 可能需要通知小部件它需要自行更新)。

public static void updateWorkoutsWidget(Context context) {
    Intent intent = new Intent(context, WorkoutsWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId);
    int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WorkoutsWidget.class));
    if(ids != null && ids.length > 0) {
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        context.sendBroadcast(intent);
    }

然后小部件有一个字段来保存 userId 并在 onReceive 中更新它。我使用用于创建收集适配器的 Intent 将其传递到 RemoteViewsService。

public class WorkoutsWidget extends AppWidgetProvider {
    private long userId;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1) != -1) {
             userId = intent.getLongExtra(WorkoutsWidgetService.USER_ID_EXTRA, -1);
        }
        super.onReceive(context, intent);
    }

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // update each of the app widgets with the remote adapter
        for (Integer widgetId : appWidgetIds) {
            Intent intent = new Intent(context, WorkoutsWidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            intent.putExtra(WorkoutsWidgetService.USER_ID_EXTRA, userId);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_workouts);
            rv.setRemoteAdapter(R.id.workouts_list, intent);

            rv.setEmptyView(R.id.workouts_list, R.id.empty_view);

            //(Pending intent stuff ommitted for brrevity)

            appWidgetManager.updateAppWidget(widgetId, rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

然后所有这些最终直接通过 RemoteViewsService 传递到它的 RemoteViewsFactory:

public class WorkoutsWidgetService extends RemoteViewsService {
    public static final String TRACK_WORKOUT_ACTION = "com.bodybuilding.mobile.service.TRACK_WORKOUT_ACTION";
    public static final String USER_ID_EXTRA = "userId";

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new WorkoutWidgetViewsFactory(this.getApplicationContext(), intent);
    }

    class WorkoutWidgetViewsFactory implements RemoteViewsFactory, ServiceConnection {

        WorkoutWidgetViewsFactory(Context context, Intent intent) {
            this.context = context;
            appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            userId = intent.getLongExtra(USER_ID_EXTRA, -1);
        }

        //Lots of other stuff in here
    }
}

关于android - 如何将数据从应用程序发送到 AppWidgetProvider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718426/

相关文章:

Android:如何将整个 ImageView 转换为位图?

java - RadioButton 值在不同 Activity 中相互重复

android - 如何创建一个根据变化的变量显示其状态的复选框?

android - 在运行时创建、存储和扩充自定义布局

android - 在android中关闭屏幕

android - 如何手动处理复选框状态?

android - 设置和使用 Gherkin Android Studio

android - 图片来自网络-加载时间

android - 如何为 View 创建 UI 等首选项

android - PreferenceActivity 验证用户输入并显示错误消息