使用警报管理器安排Void操作

时间:2016-01-28 08:23:16

标签: android alarmmanager

我想安排action一段时间,比如下午2点到晚上8点,每天执行一次,当用户打开应用程序时为此我尝试使用alarmManager但不是对于实现我的目标肯定是一个好主意,或者不是尝试这个:

    Intent mintent = new Intent(getActivity(), TabFragment4.class);
// here i am creating an intent and tabFragment4 is this class (where am setting this intent)
    mintent.setAction("Set", showCallbacks());
//here am trying to put Action which is showCalbacks but it didn't seems to work cause its not the proper way of doing this and am not able to figure how to add action properly 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, mintent, PendingIntent.FLAG_UPDATE_CURRENT);
//here is my pending intent 

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 01); // For 1 PM or 2 PM
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
//here am setting my calendar's time for 1 pm 

    AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//here am creating AlarmManger named am

    am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent );
//here am scheduling my action for repeating everyDay

我的功能,我想安排只执行一次(但每天)

    public void showCallbacks() {
    new MaterialDialog.Builder(getActivity())
            .title("Hey")
            .content("ballin in the budget ?")
            .positiveText("Yeah")
            .negativeText("Neah")
            .neutralText("")
            .onAny(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    Toast.makeText(getContext(), (which.name() + "!"), Toast.LENGTH_LONG).show();
            .show();
}

我的方法够好吗?如果是,请帮助我将此动作放入alarmManager,如果我的方法不是真正需要的,请指导我,以便我能实现我想要的感谢:)任何指导对我都有帮助,谢谢

我的broadCastReciever类:

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);



}

}

ps - 如果我的问题不够清楚,请让我知道我会解决它

1 个答案:

答案 0 :(得分:2)

如果您想在特定级别设置警报,则可以使用广播接收器和alerm管理器。

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

    }
  

在MainActivity中

private void setAlarm(Calendar targetCal){

        info.setText("\n\n***\n"
                + "Alarm is set@ " + targetCal.getTime() + "\n"
                + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
    }
相关问题