【问题标题】:AlarmManager set up Android?AlarmManager 设置Android?
【发布时间】:2012-05-10 18:34:59
【问题描述】:

我有需要每 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() 方法内编写通知代码。 帮助? 谢谢,狼。

【问题讨论】:

  • 为什么不呢?尝试时会发生什么?

标签: java android


【解决方案1】:

朋友,我为你做的事:

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();
    }

【讨论】:

    【解决方案2】:

    为什么不呢?

    @Override
    public void onReceive(final Context context, final Intent bootintent)
    {
        Intent anyService = new Intent(context, AnyService.class);
        context.startService(anyService);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多