Android应用程序中的警报问题

发布于 2024-12-28 21:10:53 字数 1800 浏览 2 评论 0原文

我有一个警报应用程序,应该每 1 分钟打印一条 toast 消息。但是当应用程序第一次运行时,它只执行一次。

这是我的代码:

 private class ConnectivityReceiver extends BroadcastReceiver{
        MainActivity  m= new MainActivity ();
            private Timer mTimer;
            private TimerTask mTimerTask;

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
                    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); 
                    wl.acquire();                               Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // Every 1 minute i print the tost message.                        wl.release();


            }
            public void SetAlarm(Context context) 
            { 
             AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Intent i = new Intent(context, ConnectivityReceiver.class); 
             PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);  
             am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
             } 
             public void CancelAlarm(Context context)  { 
             Intent intent = new Intent(context, ConnectivityReceiver.class); 
             PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
             alarmManager.cancel(sender);  
             }
                    }

i have a alarm application that should print a toast message every 1 minute.But it does that just once when Application is run for the first time.

here my code:

 private class ConnectivityReceiver extends BroadcastReceiver{
        MainActivity  m= new MainActivity ();
            private Timer mTimer;
            private TimerTask mTimerTask;

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
                    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); 
                    wl.acquire();                               Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // Every 1 minute i print the tost message.                        wl.release();


            }
            public void SetAlarm(Context context) 
            { 
             AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Intent i = new Intent(context, ConnectivityReceiver.class); 
             PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);  
             am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
             } 
             public void CancelAlarm(Context context)  { 
             Intent intent = new Intent(context, ConnectivityReceiver.class); 
             PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
             alarmManager.cancel(sender);  
             }
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2025-01-04 21:10:53

只需做一件事,

PendingIntent sender = PendingIntent.getBroadcast(context, 0, Intent, 0);

替换为 PendingIntent sender = PendingIntent.getBroadcast(context, **someUniqueId** , Intent, 0);

其中 uniqueId 是整数,您可以生成或声明为“i++”,其中 i 是 public static int i;

希望这能起作用

Just do one thing,

in the line PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);

replace with PendingIntent sender = PendingIntent.getBroadcast(context, **someUniqueId**, intent, 0);

where uniqueId is integer either u generate or declare as "i++" where i is public static int i;

Hope this will work

橘和柠 2025-01-04 21:10:53

请将您的这一行更新

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi);

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ( 1000 * 60 ), 1000 * 60 * 10, pi);

这将使您的 System.currentTimeMillis(); 添加一分钟,并将在该时间之后开始。

Please update your this line

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi);

to

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ( 1000 * 60 ), 1000 * 60 * 10, pi);

This will add one minute your System.currentTimeMillis(); and will start after that time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文