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());
}
}
Toast 和 Notification 都不显示。
AlarmManager
代码是在我的启动器 activity 中运行的第一件事,以防有帮助。谢谢
请像这样在清单文件中注册您的接收器
在应用标签中
<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 和适当的 intent FLAG 创建待处理的 intent。
2).取消那个未决的意图。
3).使用闹钟管理器取消闹钟。
我正在尝试安排使用 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());
}
}
Toast 和 Notification 都不显示。
AlarmManager
代码是在我的启动器 activity 中运行的第一件事,以防有帮助。谢谢
请像这样在清单文件中注册您的接收器
在应用标签中
<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 和适当的 intent FLAG 创建待处理的 intent。 2).取消那个未决的意图。 3).使用闹钟管理器取消闹钟。