简体   繁体   English

AlarmManager似乎不起作用

[英]AlarmManager doesn't seem to be working

I'm trying to schedule a method to be called using AlarmManager , but it doesn't seem to be working. 我正在尝试安排要使用AlarmManager调用的方法,但是它似乎没有用。 I've looked at other examples and theirs isn't working for me. 我看过其他示例,但这些示例对我没有用。 So I'm thinking it's something from my code. 因此,我认为这是我的代码中的某些内容。 Here's the AlarmManager code: 这是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);

And my broadcast receiver: 我的广播接收器:

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

Neither the Toast nor the Notification show. 吐司或通知均未显示。

The AlarmManager code is in the first thing that runs in my launcher activity, in case that helps. 如果有AlarmManager代码是在我的启动器活动中首先运行的代码。 Thanks 谢谢

Please register ur receiver in manifeast file like this 请像这样在清单文件中注册您的接收者

In application tag 在应用标签中

<receiver
   android:name=".KinectReceiver">

Second question is how to cancel alarm 第二个问题是如何取消警报

Here is ur answer 这是你的答案

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

The main thing that you will need is: 您将需要的主要内容是:

1).Create pending intent with the same id and appropriate intent FLAG. 1)。创建具有相同ID和适当意图FLAG的待定意图。 2).Cancel that pending intent. 2)。取消该未决意图。 3).Cancel the alarm using alarm manager. 3)。使用警报管理器取消警报。

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

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