我尝试安排使用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
代码是我的启动器活动中运行的第一件事,如果有帮助的话。感谢
答案 0 :(得分:1)
请在这样的表现文件中注册你的收件人
在应用程序标签
中<receiver
android:name=".KinectReceiver">
答案 1 :(得分:1)
第二个问题是如何取消闹钟
这是你的回答
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)。使用报警管理器取消报警。