开发者

Display alertDialog on alarm?

I would like to display an alert dialog when the alarm g开发者_如何学编程oes off. Here is where i am so far. Im not sure if im doing it right.

@Override
    void doTaskWork(Intent intent){
        String taskId = intent.getStringExtra(TaskHelper._id);

        NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, TaskDetails.class);
        notificationIntent.putExtra(TaskHelper._id, taskId);
        PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
        Notification note = new Notification(R.drawable.stat_sys_warning, );

    }


}


Alarm:

You can schedule a pending intent that drives what you want when the alarm fires. The process is:

Determine how often you want the alarm to fire. You can fire at an exact time, a specific time from now (in 10 seconds..), or a specific repeat at an interval (every x seconds/minutes/etc.). You can also set a specific time to start the repeat process. The interval isn't variable. Then you have to do one shots and set another alarm for the next time. You can also set flags that determine the time format (millis, RTC, ...). Finally, you can have the alarm firing wake up the device or let it sleep and get scheduled the next time the phone is awake.

Now, as to what is scheduled. A pending intent is scheduled. The pending intent wakes up a broadcast receiver. Here's some clips of code I use to fire a timer at 1 minute past midnight daily. (It updates a widget that has to update daily.)

    Intent intent = new Intent(context, DaysReceiver.class);
    PendingIntent receiverIntent = PendingIntent.getBroadcast(context,
          DaysConstants.UPDATE_ALARM,
          intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Schedule the alarm!
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(receiverIntent);
    if (cancelAlarm) {
        MyLog.d(TAG, "setAlarm cancel");
        return;
    }
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    JodaTime jtime = new JodaTime();
    am.set(AlarmManager.RTC_WAKEUP, jtime.afterMidnight(), receiverIntent);
     //am.setRepeating(AlarmManager.RTC_WAKEUP, jtime.nowPlusMillis(30 * 1000),
     //  30 * 1000,     receiverIntent);
    MyLog.d(TAG, "setAlarm set");
}

The JodaTime class does date and time calculations. the afterMidnight() bit above returns 1 minute after midnight tonight. The routine can be used to just cancel an outstanding alarm.

The receiver is just a normal broadcast receiver and you can do anything in it that you can do in any other broadcast receiver. (Don't forget to put the usual stuff in the manifest. Permissions, and such like.

Here's the receiver I'm using less the imports. It's pretty straight forward. It grabs all the widgets that are on home screens and updates them. The update routine is a static function in the widget provider. It's a class because it is driven from two places. The widget config and the widget provider. The timer is rescheduled every 24 hours. The alarm won't live through a boot, but the provider's on update is driven at reboot. (All that's happening is the new day calculations are performed and the widget display is updated.) You could drop my code and put in a startActivity. Ooops. Almost forgot. Use PendingIntent.FLAG_UPDATE_CURRENT so you don't have multiple intents stacked up accidentally...

    public class DaysReceiver extends BroadcastReceiver {

static String TAG = "DaysReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    MyLog.d(TAG, "onReceive");
    updateWidgets(context);
}

private void updateWidgets(Context context) {
    MyLog.d(TAG, "updateWidgets");
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName componentName = new ComponentName(context, DaysProvider.class);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
    final int N = appWidgetIds.length;
    if (N < 1) {
        MyLog.d(TAG, "No widgets");
        return;
    }
    for (int i = 0; i < N; i++) {
        MyLog.d(TAG, "Update widget " + Integer.toString(appWidgetIds[i]));
        DaysProvider.updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
    }
}

}

Hope I haven't rambled to much, but I'm in a rush to get back to some other business. I don't have the time to really edit the post. Hope this helped...


Do you really need a notification? Why not fire off an activity that can do the alarm notification and disappear. You can sound an alarm, vibrate the phone, whatever. Even do a notification if you still want to...

    Intent intent = new Intent(context.MyAlarmResponse);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("REASONFORALARM", "What ever you want");
    context.startActivity(intent);

In the manifest, use the following theme to look like a dialog:

    <activity android:name=".MyAlarmResponse"
        android:theme="@android:style/Theme.Dialog">
    </activity>

It doesn't have to look like a dialog. You can do a full court press with a full screen display, animation, vibrate, and sound. The user than hits your cancel key and it all goes away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜