0
我仍然處於學習使用Java進行Android編程的第一步。 所以我的問題可能對一些更有經驗的程序員來說是顯而易見的,但我無法找到任何有用的或特定的示例。僅在時間流逝時發送意圖onUpdate
無論如何,我有一個小部件。我想在我的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方法只有當我的計時器過去了。
感謝提前:)