广播接收器中的报警管理器

时间:2014-01-14 11:00:54

标签: broadcast

可以在一个没有活动的类中使用带有广播接收器的报警管理器,如下所示:

public class AlarmReceiver extends BroadcastReceiver {

private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";

@Override
public void onReceive(Context context, Intent intent) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, 43);
    calendar.set(Calendar.SECOND, 0);

    Intent intent1 = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1,
            intent1, 0);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            pendingIntent1);

    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    myNotification = new NotificationCompat.Builder(context)
            .setContentTitle("Exercise of Notification!")
            .setContentText("http://android-er.blogspot.com/")
            .setTicker("Notification!").setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_launcher).build();

    notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}

}

我想在没有app的情况下使用这个Alarm Manager。

1 个答案:

答案 0 :(得分:0)

without app on,没有Activity且没有Service。不,你不能。您需要一个上下文来定义您的广播接收器。您可以使用活动或服务或应用程序的上下文。

你可以像这样创建类,但是你应该在活动的某个地方实例化它......