0

我仍处于学习用 Java 为 Android 编程的第一步。所以我的问题对于一些更有经验的程序员来说可能很明显,但我真的找不到任何有用的或具体的例子。

无论如何,我有一个小部件。我想在调用我的 onUpdate 时发送通知,因为 android:updatePeriodMillis 已经发生。我知道如何在使用 setOnClickPendingIntent 单击按钮时获得通知。但是只有当计时器用完时,我才知道该怎么做。

我的一些代码:

@Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_app_widget);
        Intent configIntent = new Intent(context, FrontPage.class);
        configIntent.setAction(ACTION_WIDGET_CONFIGURE);
        Intent active = new Intent(context, AppWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        active.putExtra("msg", "Message for Button 1");

        Intent activeButtonTwo = new Intent(context, AppWidget.class);
        activeButtonTwo.setAction(ACTION_WIDGET_RECEIVER2);
        activeButtonTwo.putExtra("msg", "Message for Button 2");

        Intent updateNotification = new Intent (context, AppWidget.class);
        updateNotification.setAction(ACTION_UPDATE);

        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, activeButtonTwo, 0);
        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.buttonPrevious, actionPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.buttonNext, actionPendingIntent2 );
        remoteViews.setOnClickPendingIntent(R.id.buttonGoToApp, configPendingIntent);





        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

    }

这是我的 onReceive():

@Override    
public void onReceive(Context context, Intent intent)
        {   

            // v1.5 fix that doesn't call onDelete Action
                    final String action = intent.getAction();
                    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
                        final int appWidgetId = intent.getExtras().getInt(
                                AppWidgetManager.EXTRA_APPWIDGET_ID,
                                AppWidgetManager.INVALID_APPWIDGET_ID);
                        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                            this.onDeleted(context, new int[] { appWidgetId });
                        }
                    } else {
                        // check, if our Action was called
                        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER) || intent.getAction().equals(ACTION_WIDGET_RECEIVER2)) {
                            String msg = "null";
                            try {
                                msg = intent.getStringExtra("msg");
                            } catch (NullPointerException e) {
                                Log.e("Error", "msg = null");
                            }
                            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

                            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
                            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                            Notification noty;
                            if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)){
                                noty = new Notification(R.drawable.ic_launcher, "Button 1 clicked", System.currentTimeMillis());
                            }
                            else
                            {
                                noty = new Notification(R.drawable.ic_launcher, "Button 2 clicked", System.currentTimeMillis());
                            }
                            noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
                            notificationManager.notify(1, noty);

                        } else {
                            // do nothing
                        }

                        super.onReceive(context, intent);
                    }
    }

所以长话短说,我只想在我的计时器到时才向我的 onReceive 方法发送一个特定的 Intent。

提前致谢 :)

4

1 回答 1

0

如果您不想在设备休眠时更新,最好使用 AlarmManager。(参见http://developer.android.com/guide/topics/appwidgets/index.html)例如,像这样:

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + YOUR_UPDATE_INTERVAL,
                YOUR_UPDATE_INTERVAL, intent);  

intent 是您设置 Action 的 PendingIntent。

于 2012-11-06T13:47:05.973 回答