我还在学习用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。
提前致谢:)
答案 0 :(得分: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。