在Android上运行闹钟后台服务?

发布于 2024-12-02 07:31:18 字数 90 浏览 1 评论 0原文

在我的应用程序中,用户可以设置特定时间,我希望当时间达到设置时间(基本上是警报)时,无论应用程序当前是否正在运行,屏幕上都会显示通知。

我该怎么做呢?

Within my application a user can set a certain time, i wish for a notification to be displayed on the screen when the time reaches the set time (basically an alarm) out wheather the application is currently running or not.

How would i go about doing this?

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

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

发布评论

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

评论(3

残龙傲雪 2024-12-09 07:31:18

进入长答案是没有用的,你显然没有搜索太多......

http ://developer.android.com/reference/android/app/AlarmManager.html

No use getting into a long answer, you obviously didn't search much...

http://developer.android.com/reference/android/app/AlarmManager.html

偏爱你一生 2024-12-09 07:31:18

现在检查一下,我已经更新了代码。

这是 CountDownTimer 的代码。
当时间结束时,它会发送通知。
您可以在 Service 或普通 Activity 中使用它。

     public String ns =  Context.NOTIFICATION_SERVICE; 
        public NotificationManager mNotificationManager;    
        Notification notification;
        PendingIntent contentIntent;
        Intent notificationIntent;

        public class Main extends Activity{
            private final long startTime = 50000;
            private final long interval = 1000;

            @Override
            public void onCreate(Bundle savedInstanceState)
                {
                    super.onCreate(savedInstanceState);
                            int time=Integer.parseInt(editText.getText().toString());
                            time=time*1000;
                            startTime = time;

                            // place this code on button listener if you want.
                    countDownTimer = new MyTimer(startTime, interval);
                }
        }

        public class MyTimer extends CountDownTimer
        {
            public MyTimer(long startTime, long interval)
                {
                    super(startTime, interval);
                }

                @Override
                public void onFinish()
                {
                    mNotificationManager  = (NotificationManager) getSystemService(ns);
                    notificationIntent = new Intent(this, MyActivity.class); 
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                    notification = new Notification(R.drawable.icon,"Time up..", System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "CallingaCab", "YOUR TIME COMPLETED", contentIntent);
                    mNotificationManager.notify(0, notification);
                    startActivity(notificationIntent);
                }

                @Override
                public void onTick(long millisUntilFinished)
                {
                    //Perform what do you want on each tick.
                }
            }
        }

Check now, I have updated the code.

Here is the code for CountDownTimer.
and when time completes it sends an notification.
You can use it in Service or in a normal Activity.

     public String ns =  Context.NOTIFICATION_SERVICE; 
        public NotificationManager mNotificationManager;    
        Notification notification;
        PendingIntent contentIntent;
        Intent notificationIntent;

        public class Main extends Activity{
            private final long startTime = 50000;
            private final long interval = 1000;

            @Override
            public void onCreate(Bundle savedInstanceState)
                {
                    super.onCreate(savedInstanceState);
                            int time=Integer.parseInt(editText.getText().toString());
                            time=time*1000;
                            startTime = time;

                            // place this code on button listener if you want.
                    countDownTimer = new MyTimer(startTime, interval);
                }
        }

        public class MyTimer extends CountDownTimer
        {
            public MyTimer(long startTime, long interval)
                {
                    super(startTime, interval);
                }

                @Override
                public void onFinish()
                {
                    mNotificationManager  = (NotificationManager) getSystemService(ns);
                    notificationIntent = new Intent(this, MyActivity.class); 
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                    notification = new Notification(R.drawable.icon,"Time up..", System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "CallingaCab", "YOUR TIME COMPLETED", contentIntent);
                    mNotificationManager.notify(0, notification);
                    startActivity(notificationIntent);
                }

                @Override
                public void onTick(long millisUntilFinished)
                {
                    //Perform what do you want on each tick.
                }
            }
        }
帝王念 2024-12-09 07:31:18

请创建服务以扩展在后台运行的服务类,并检查您想要的内容。 在这里您可以找到服务示例

Please create service to extends Service class that service run on background and check what ever you want to. here you can find service example.

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