如何从活动中向窗口小部件发送时间?

时间:2012-12-10 07:57:42

标签: android android-widget

我可以创建一个显示祷告时间的应用程序。我希望这也显示小部件上的时间可以任何人告诉我我可以制作小部件,现在的问题是如何在小部件上发送数据我完全混淆了如何在小部件上发送时间?/

3 个答案:

答案 0 :(得分:1)

将此代码写入您的小部件appwidgetprovider

public void onReceive(Context context, Intent intent) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget);
    Intent intent = new Intent();
    if (intent.getAction().equals(
            Constants.ACTION_WIDGET_UPDATE_FROM_ACTIVITY)) {
        String widgetText = intent.getExtras().getString(
                Constants.INTENT_EXTRA_WIDGET_TEXT);
        remoteViews.setTextViewText(R.id.word_text, widgetText);
    } else {
        remoteViews.setTextViewText(R.id.word_text, showPrayer);
    }
           `  }

答案 1 :(得分:1)

尝试这可能会对你有所帮助

public void onReceive(Context context,Intent intent){

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
        R.layout.widget);
Intent intent = new Intent();
if (intent.getAction().equals(
        Constants.ACTION_WIDGET_UPDATE_FROM_ACTIVITY)) {
    String widgetText = intent.getExtras().getString(
            Constants.INTENT_EXTRA_WIDGET_TEXT);
    remoteViews.setTextViewText(R.id.word_text, widgetText);
} else {
    remoteViews.setTextViewText(R.id.word_text, showPrayer);
}
       `  }

答案 2 :(得分:0)

在您的活动中尝试此代码....

   if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                AppWidgetManager appWidgetManager = AppWidgetManager
                        .getInstance(context);
                RemoteViews remoteViews = new RemoteViews(context
                        .getPackageName(), R.layout.widget_layout);


                remoteViews.setTextViewText(R.id.txt_time, ""
                        + YOURTIME);



                appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        appWidgetId);
                setResult(RESULT_OK, resultValue);
            }