活动开始时如何停止AlarmManager

编程入门 行业动态 更新时间:2024-10-27 08:37:17
本文介绍了活动开始时如何停止AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是android的新手.今天,我尝试为我的Reminder应用程序使用AlarmManage,该应用程序允许用户输入小时和分钟来显示通知,但我对此有一些问题.1.第一个问题是通知出现时,虽然我为通知设置了声音默认设置,并为手机设置了允许声音,但它只是振动而没有声音.2.当我关闭应用程序时,如果我触摸通知,将会显示MainActivity并且AlarmManager再次运行,这会使我的应用程序再次显示通知.但是,如果我在应用程序打开时触摸通知,则通知不会显示.所以我希望我的申请只通知一次.我该怎么办?3.虽然我设置了提醒时间,但是当我关闭应用程序时,有时我的手机会出现我从未设置过的提醒应用程序的通知.我该如何解决这个问题?

I'm a newbie in android. Today I tried working with AlarmManage for my Reminder application that allow users can input hour and minutes to show notification and I have some problem with it. 1. The first problem is when the notification appear, it just vibrate and didn't have sound although I have set sound default for notification and setting allow sound for my phone. 2. when I close the application, if i touch at notification, the MainActivity will be show and AlarmManager working again that make my application show notification again. But if i touch at notification when the application opened, The notification won't show. so I want my application just notification once time. How I can do it ?. 3. Although I set time for reminding, but when i closed application, sometime my phone appear notification of my remind application at time i have never set. How can i fix this problem ?

MyAlarmService

MyAlarmService

private NotificationManager mManager; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @SuppressWarnings("static-access") @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); // Toast.makeText(this, "I'm running", Toast.LENGTH_SHORT).show(); long[] v = {500,1000}; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setVibrate(v) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setSmallIcon(R.drawable.ic_remind) .setContentTitle("My notification") .setContentText("This is a test message!"); mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); mBuilder.setAutoCancel(true); Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class); intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(intent1); PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingNotificationIntent); mManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mManager.notify(0, mBuilder.build()); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); }

接收器

public void onReceive(Context context, Intent intent) { Intent service1 = new Intent(context, MyAlarmService.class); context.startService(service1); }

MainActivity

MainActivity

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText edt_hours = (EditText)findViewById(R.id.edt_hour); final EditText edt_minutes = (EditText)findViewById(R.id.edt_minutes); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(edt_hours.getText().toString())); calendar.set(Calendar.MINUTE, Integer.parseInt(edt_minutes.getText().toString())); //calendar.set(Calendar.SECOND,0); Intent myIntent = new Intent(MainActivity.this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0); alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } }); } //end onCre

p/s:如果我的问题不清楚,我很抱歉,因为我的英语不好.我想对任何阅读此问题的人表示感谢.

p/s: I'm so sorry if my question is not clearly because my english is not good. And I want to say thanks you for anyone who read this question.

推荐答案

这是我的示例代码.我认为它将为您提供帮助.

Here is my sample code. I think It will help you.

MainActivity

MainActivity

public static int NOW_NOTIFICATION = 101; Intent intent = new Intent(MainActivity.this, NotificationReceiver.class); intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert"); PendingIntent pendingIntent = PendingIntent.getBroadcast( MainActivity.this, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT); cancelNotification(NOW_NOTIFICATION); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);

要取消通知:

public void cancelNotification(int requestCode) { try { Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); } catch (Exception e) { e.printStackTrace(); } }

NotificationReceiver:

NotificationReceiver:

public class NotificationReceiver extends BroadcastReceiver { public static String NOTIFICATION_MESSAGE = "notificationMessage"; @Override public void onReceive(Context context, Intent intent) { String message = intent.getStringExtra(NOTIFICATION_MESSAGE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //Define sound URI Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Intent notificationIntent = new Intent(context, EmptyActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); final DateTime dateTime = DateTime.now(); int color = 0xffffaa00; // int color1 = context.getColor(R.color.notificatinBackgroundColor); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.festi_push_message_small); builder.setContentIntent(pendingIntent); builder.setContentTitle("Notification Sample"); builder.setAutoCancel(true); builder.setContentText(message + ", Current Time : " + dateTime.getHourOfDay() + ":" + dateTime.getMinuteOfHour() + ":" + dateTime.getSecondOfMinute()); builder.setSound(soundUri); builder.setLights(Color.RED, 1000, 1000); builder.setColor(color); Notification notification = builder.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(102938, notification); } }

AndroidManifest.xml

AndroidManifest.xml

<receiver android:name=".NotificationReceiver" />

更多推荐

活动开始时如何停止AlarmManager

本文发布于:2023-11-27 01:19:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!