Is it possible to make an installed app auto start itself?
thanks
Read this article if you want to start an application on boot up.
Yes .
For this you have to use Alarm Notification .
You can found many tutorial for this on Net. Just google it..
You will found code something like this...
Intent intent = new Intent(FirstScreen.this, MyBroadcastListener.class);
PendingIntent sender = PendingIntent.getBroadcast(FirstScreen.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
The BroadCastListener You will set can call any activity in return. of own app too..:)
Auto Start on any event? Check the IntentFilter out to make your app default for particular actions.
Auto Start on/after a particular time? check Alarm out!
Related
I created a service that will check some online data in specific time of the day and everyday (for example at 6 AM), and rather than using a background running service and broadcast receiver I decided to use AlarmMananger and the code below:
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
My questions are:
1- How to unregister from the AlarmManager when user decided to de-activate my service?
2- What is the use of this line
calendar.setTimeInMillis(System.currentTimeMillis());
3- What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?
4- Is it possible to check AlarmMananger if it has successfully registered my Intent?
Edit 1:
The code below is not triggering my broadcast receiver, why?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6); // For 6 AM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, mybroadcastreceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
AndroidManifest.xml
<receiver
android:name=".mybroadcastreceiver"
android:enabled="true"
android:exported="true"></receiver>
Coming to all your questions :-
How to unregister from the AlarmManager when user decided to de-activate my service?
Answer :- To cancel an AlarmService you created, you need to cancel it like this.
this.getAlarmManager().cancel(pendingIntent);
where this is the service (or Activity/Context) from where you called the AlarmService.
What is the use of this line
calendar.setTimeInMillis(System.currentTimeMillis());
Well if you are aware of the method Date.setTime(), which generates the Date object with milliseconds provided in the argument. Calender.setTimeInMillis() does the same for the Calender object. System.currentTimeMillis() is used to provide the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. ( I guess you are aware of whole Timestamp concept)
3. What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?
As the documentation says, If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.
4. Is it possible to check AlarmMananger if it has successfully registered my Intent?
I think it is possible, but there is no such method to check it directly, you need to do the hack. I think you will find this answer suitable.
I hope this helps you :)
You can use if else function for register and unregister from AlarmManager.
for example checkBox isSelectted() or not.
calendar.setTimeInMillis(System.currentTimeMillis());
this line will return you system time in millisecond format. This format is like this "2547889955511"
yes if you can perfectly set up AlarmManager your pending intent can call your service twice.
yes you have to set up perfectly then it will possible to check AlarmMananger is it successfully registered your Intent or not.
I have a database in which the user saves the things that he wants to do.In each thing he can set a date of expiration.the point is, I want to create a list(notification list) where the "things to do" that are going to expire will appear. for example the user wants to create a list with the things that are going to expire in two days. I have used the IntentService and pass with the intent the time that the user selects each time but i get nothing. do I have to use service or bound service in this case? thank you in advance
you can use AlarmManager class to fire off events at given times and in these events you can use NotificationManager to trigger the notifications.
See alarm manager example here:
Alarm Manager Example
See notifications guide here:
https://developer.android.com/guide/topics/ui/notifiers/notifications.html
Use the AlarmManager technique as like,..
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, yearFromDB);
calendar.set(Calendar.MONTH, monthFromDB);
calendar.set(Calendar.DAY_OF_MONTH, dateFromDB);
calendar.set(Calendar.HOUR_OF_DAY, hourFromDB);
calendar.set(Calendar.MINUTE, minuteFromDB);
calendar.set(Calendar.SECOND, 0);
Use PendingIntent for the Wake up at the Specific time and perform this Task..
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), ObjPendingIntent);
You can get the to do list on the Particular Time, Read on the PendingIntents and Alarmmanager by the following links..
http://developer.android.com/reference/android/app/PendingIntent.html
and
http://developer.android.com/reference/android/app/AlarmManager.html
Well I am trying to develop a memo or notebook app with a new feature of reminding the users in certain date and time they set.
I used datepicker and timepicker to pick up date and time, and I use alarmmanager to set an alarm to notify the users, but I don't know how to check the date, could anyone help me?
THX
PS, is there any opensource android apps about it? I would like to know
You can use AlarmManager class to schedule reminders. It allow you to schedule your application to be run at some point in the future. Here's a sample code to get you started:
Intent intent = new Intent(this, YourMemoService.class);
PendingIntent pending = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, Time_To_wake, pending);
In my application i want to use alarm service for specific period of time.I'm taking start time and end time values from user and saving it in database,Now i want to start a alarm service at start time and alarm should go off at end time specified by user.I'm new to this topic and not able to understand how to implement this...Any help will be appreciated.Thank u..
This is how you implement an alarm manager. But you will need to read about Calendar object in android also.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);//Just an example setting the alarm for the 8th hour of a day.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
//This is the intent that is launched when the alarm goes off.
Intent intent = new Intent("WAKE_UP");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
//If the user wants the alarm to repeat then use AlarmManager.setRepeating if they just want it one time use AlarmManager.set().
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, sender);
}
Also you will need to register a BroadCast Receiver to except the intent when the alarm sets it off.
You create the BroadCast reciever and register it in your manifest to receive the intent from the alarm.
http://www.vogella.de/articles/AndroidServices/article.html
Here is a great tutorial to help you understand better
The key is to use the AlarmManager with a pending intent.
mAlarmSender = PendingIntent.getService(AlarmService.this,
0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
Then you create the AlarmManager from the current context:
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
And schedule the previously created pending intent.
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 30*1000, mAlarmSender);
On schedule the AlarmService_Service service will be called, or you can put another intent like open a specific activity.
Here is the complete example of how you can schedule an alarm: AlarmService.java
I am trying to use this to set an alarm that goes off everyday.
String alarm = Context.ALARM_SERVICE;
Calendar calendar = Calendar.getInstance();
AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm);
Intent intent = new Intent("NEW_ITEM");
PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
calendar.setTimeInMillis(System.currentTimeMillis());
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1*AlarmManager.INTERVAL_DAY, sender);
Without running it, the code looks good to me... Obviously, if you set this alarm every time you start the activity, the alarm will go off immediately since: am.setRepeating(AlarmManager.RTC_WAKEUP, **calendar.getTimeInMillis()**, 1*AlarmManager.INTERVAL_DAY, sender); Tells the alarm manager to alert right now (2nd param) and to repeat in a day (3rd param, assuming your constant is correct).
If you want the alert to start only in 24 hours, simply change the line to:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, sender);
Code looks good, but you have to be aware of one thing. If the user decides to set the alarm again (for example, by hitting the 'set the alarm' button) the old one will be replaced. If you want to avoid this, check out this topic: Using Alarmmanager to start a service at specific time