3

私の友人は 1 つのアプリケーションを完成させ、今では彼のアプリケーション用に 1 つのウィジェットを作成しました。これを使用してリスト ビューでタスクのリストを表示できます。最初に、ウィジェットをドラッグしてホーム画面にインストールすると、正常に機能し、タスクがリスト ビューで表示されます。

彼の問題は、アプリケーションに変更を加えてからホーム画面に戻ったときに、変更が反映されないことです。彼はonReceive()このメソッドを使用してウィジェットを更新しています

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetTaskSchedular extends AppWidgetProvider {
    private final String TAG = "CalendarViewSample:"
            + this.getClass().getName();

    SharedPreferences sharedprefer;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if(intent.getAction().equals("update_widget"))
        {
            int[] appid=new int[1];
            sharedprefer=context.getSharedPreferences("TaskWidget", 0);
            appid[0]=sharedprefer.getInt("widget_key",0);
            Log.i(TAG,"Appwidgetid"+appid[0]);
            onUpdate(context, AppWidgetManager.getInstance(context),appid);

        }
    }
    public static String EXTRA_WORD=
            "com.capsone.testing.calendar.WORD";
    @SuppressWarnings("deprecation")
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
         for (int i=0; i<appWidgetIds.length; i++) {

             sharedprefer=context.getSharedPreferences("TaskWidget", 0);
             SharedPreferences.Editor editor=sharedprefer.edit();
             editor.putInt("widget_key", appWidgetIds[i]);
             editor.commit();
             int a= sharedprefer.getInt("widget_key", 0);
             Log.i(TAG,"Sharedpreferencewidgetid:"+a);

              //ID=appWidgetIds[i];
              Log.i(TAG,"LengthofWidget:"+appWidgetIds.length);
             // Log.i(TAG,"TestAppWidget:"+ID);
              Intent intentWidgetService=new Intent(context, WidgetService.class);
              intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));

              RemoteViews remoteView=new RemoteViews(context.getPackageName(),
                                                  R.layout.widgetlayout);

              remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget,
                                      intentWidgetService);

              Intent clickIntent=new Intent(context, ActionBarActivity.class);
              PendingIntent clickPendingIntent=PendingIntent
                                      .getActivity(context, 0,
                                                    clickIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
              remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent);
             ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
              appWidgetManager.updateAppWidget(component, remoteView);
            }

    }
}
4

2 に答える 2

0

ここに記載されている解決策を試しましたか:

ウィジェットを動的に更新する方法 (onUpdate が呼び出されるまで 30 分待たない)?

上記は更新をトリガーするだけであり、ウィジェット自体が必要なデータをフェッチする必要があることに注意してください。

ブロードキャスト レシーバーをウィジェットに登録することはできますが、更新後はガベージ コレクションの対象になるため、信頼できるソリューションではないことに注意してください。

最後の代替手段は、AlarmManager を登録して、ウィジェットの更新を実行する Service を起動することです。

于 2013-09-05T07:38:52.830 に答える