java - Android AlarmManager设置?

标签 java android

我有警报管理器,需要每 12 小时重复一次。

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent in = new Intent(this, myReceiver.class);
        myAlarmSender = PendingIntent.getBroadcast(getApplicationContext(), 0, in, 0);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time, myAlarmSender);

发生这种情况后,我在类 myReceiver 中捕获触发事件,该类扩展了广播接收器

@Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "text", Toast.LENGTH_LONG).show();
    }

现在我想设置状态栏通知或启动我的通知类,其中有通知代码,但我无法启动新的 Intent 或在 onReceive() 方法内部编写通知代码。 帮助? 谢谢,狼。

最佳答案

我的 friend ,我为你做的事情:

http://blog.blundell-apps.com/notification-for-a-user-chosen-time/

您将希望 AlarmManager 在启动服务时设置闹钟。然后,当您的服务启动时,您可以显示一条通知。

设置闹钟:

public class AlarmTask implements Runnable{
    // The date selected for the alarm
    private final Calendar date;
    // The android system alarm manager
    private final AlarmManager am;
    // Your context to retrieve the alarm manager from
    private final Context context;

    public AlarmTask(Context context, Calendar date) {
        this.context = context;
        this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.date = date;
    }

    @Override
    public void run() {
        // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(context, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

这将启动一个服务NotifyService

然后在NotifyService类中:

private void showNotification() {
        // This is the 'title' of the notification
        CharSequence title = "Alarm!!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Your notification time is upon us";
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, notification);

        // Stop the service when we are finished
        stopSelf();
    }

关于java - Android AlarmManager设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540201/

相关文章:

java - JSONObject 包含转义字符

java - Spring Batch条件流不执行else部分

java - 使用 Google map 检测我的应用中的其他设备

android - 获取 Skobbler 中的当前位置,但只有一次

android - 像原生相机一样编码我的 android 闪光相机

java - Eclipse:如何提示用户选择一个应用程序来打开文件?

java - java中如何计算给定字符串中的重复子字符串

java - 一条消息可以有多个发件人吗?

android - SQLDelight 迁移

java - android:TimePickerDialog 阻止用户选择过去的时间,并可以选择具有新日期的 future 时间