开发者

running a background service for an alarm on android?

开发者 https://www.devze.com 2023-03-31 19:52 出处:网络
Within my application a user can set a certain time, i wish for a notifi开发者_如何学Pythoncation to be displayed on the screen when the time reaches the set time (basically an alarm) out wheather the

Within my application a user can set a certain time, i wish for a notifi开发者_如何学Pythoncation 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?


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

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


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.
                }
            }
        }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号