繁体   English   中英

AlarmManager似乎不起作用

[英]AlarmManager doesn't seem to be working

我正在尝试安排要使用AlarmManager调用的方法,但是它似乎没有用。 我看过其他示例,但这些示例对我没有用。 因此,我认为这是我的代码中的某些内容。 这是AlarmManager代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

我的广播接收器:

public class KinectReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_notify)
                .setContentTitle("Kinect")
                .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setWhen(System.currentTimeMillis())
                .setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
                .setAutoCancel(true)
                .setSound(uri)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
                .setVibrate(new long[] { 100, 500, 100, 500, 100 });

        Intent targetIntent = new Intent(context, MainActivity.class);
        targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        targetIntent.putExtra("action", "main");
        targetIntent.putExtra("id", "");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

吐司或通知均未显示。

如果有AlarmManager代码是在我的启动器活动中首先运行的代码。 谢谢

请像这样在清单文件中注册您的接收者

在应用标签中

<receiver
   android:name=".KinectReceiver">

第二个问题是如何取消警报

这是你的答案

Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
   pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
       id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   pendingIntent.cancel();
   alarmManager.cancel(pendingIntent);

您将需要的主要内容是:

1)。创建具有相同ID和适当意图FLAG的待定意图。 2)。取消该未决意图。 3)。使用警报管理器取消警报。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM