0

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

4

2 回答 2

1

为什么不?

@Override
public void onReceive(final Context context, final Intent bootintent)
{
    Intent anyService = new Intent(context, AnyService.class);
    context.startService(anyService);
}
于 2012-05-10T18:40:12.500 回答
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();
    }
于 2012-05-10T18:42:06.650 回答