Виджет Android onReceive разве обслуживание вызовов не может классифицировать?

У меня есть widgetProvider. У меня также есть класс обслуживания, который делает вычисление для виджета.

Виджет хорошо работает при инициализации. Я получаю ошибку, когда я пытаюсь назвать сервис из onReceive метода после многоадресного события, на которое я хочу воздействовать (изменение времени).

Как я могу обновить виджет от onReceive после получения многоадресного события?

Вот мой виджет

public class HijriWidget extends AppWidgetProvider{


...

        @Override 

        public void onReceive(Context context, Intent intent) {
            if(Intent.ACTION_TIME_CHANGED.equals(intent.getAction())||Intent.ACTION_DATE_CHANGED.equals(intent.getAction())){
                //THE CODE BELOW LEADS TO AN EXCEPTION. HOW CAN I UPDATE THE WIDGET HERE?
                RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);

                appWidgetManager.updateAppWidget(appWidgetId, remoteView);
            }

               super.onReceive(context, intent);

        }

        @Override

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

            for (int appWidgetId : appWidgetIds) {

                PendingIntent updatepending = HijriWidget.makeControlPendingIntent(context,RefreshService.UPDATE, appWidgetId);
                pi = updatepending;
                try {
                    updatepending.send();
                } catch (CanceledException e) {
                  e.printStackTrace();
                }

                            setAlarm(context, appWidgetId, 5*60*1000);
            }


                super.onUpdate(context, appWidgetManager, appWidgetIds);

        }

        public static PendingIntent makeControlPendingIntent(Context context, String command, int appWidgetId) {

            Intent active = new Intent(context,RefreshService.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("hijriwidget://widget/id/#"+command+appWidgetId), String.valueOf(appWidgetId));
            active.setData(data);
            return(PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT));

        }


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

            PendingIntent newPending = makeControlPendingIntent(context,RefreshService.UPDATE,appWidgetId);
            AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();


         calendar.add(Calendar.DAY_OF_YEAR, 1);
         calendar.set(Calendar.HOUR_OF_DAY, 0);
         calendar.set(Calendar.MINUTE, 0);
         calendar.set(Calendar.SECOND, 0);

            if (updateRate >= 0) {
                alarms.set(  AlarmManager.RTC, calendar.getTimeInMillis(),  newPending);


                //alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRate, newPending);
            } else {
                // on a negative updateRate stop the refreshing
                alarms.cancel(newPending);
            }
        }

}

и вот мой сервис

public class RefreshService extends Service {



@Override
public void onStart(Intent intent, int startId) {


    int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);


    RemoteViews remoteView = new RemoteViews(getApplicationContext() .getPackageName(), R.layout.widget);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    mMonth++;

    HijriCalendar hc =new HijriCalendar( mYear,mMonth,mDay);
    remoteView.setTextViewText(R.id.TextView02,hc.getHijriMonth()+"/"+hc.getHijriDay());
    remoteView.setTextColor(R.id.TextView02, Color.BLACK);
    remoteView.setTextViewText(R.id.TextView01, hc.getHijriYear()+"");
    remoteView.setTextColor(R.id.TextView01, Color.WHITE);
    appWidgetManager.updateAppWidget(appWidgetId, remoteView);
    super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

Любая справка будет цениться.Спасибо.

ОБНОВЛЕНИЕ:

Я нашел проблему. Проверьте мой ответ ниже.

7
задан Majjoodi 6 March 2011 в 15:41
поделиться