I want to open my application automatically with some intervals if user destroy my application.How can i create this.I don't know how to create this Please anybody help me..
Use AlarmManager:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Replace MyActivity.class with the activity class you want to run periodically
Intent i = new Intent(context, MyActivity.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
long now = System.currentTimeMillis();
long interval = 60 * 60 * 1000; // one hour
am.setRepeating(AlarmManager.RTC_WAKEUP, now, interval, pi);
Also, add this permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
What you can do is, Use background service.
from service you can start your application as required.
use this from service
Intent intent = new Intent(getBaseContext(), myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
for service reference
http://www.vogella.com/articles/AndroidServices/article.html
Use alarm manager (AM) or GCM. With AM you can set timer and start your app in some interval. Via GCM you can send notifications from your server and run some actions.
Using service isnt a good idea. AM and GCM is more reliable way. I think AM is what you need.
P.S. Dont do it. Users will curse you )
Related
I dont now if question already exists but i really need help. I need to upload data in intervals (hour, daily etc.). Interval information comes from the spinner (put to SharedPreferences) and when the interval is picked, timer(Handler) needs to start count. The main problem is how to make broadcast receiver to sart on spinner itemchecked, from main activity? Maybe its better way to start it with service, but how to start broadcast on spinner item selected in service. I hope someone understands the problem.
You could write the code that handles the uploads as a service and then start that service via the AlarmManager
http://developer.android.com/reference/android/app/AlarmManager.html
Intent intent = new Intent(this, myUploadService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, yourWakeUpTime, interval, pintent);
so the intent will be your service.
yourWakeUpTime is the time when the service should start first.
interval will be the time selected from your spinner.
I have a big problem with my app for several days now. I appologize if my english is not so native in advance. I implemented an AlarmManager to give the user of my app the option to start a certain service at any time of the current or the next day. So e.g. the user might choose to set the time for my service to tomorrow at 08:00 a.m. and then starts the service.
The alarm manager should now wait the calculated time from now till the chosen time (i calculated the time also manually and it is correct!) and then start the service. My problem now is that sometimes the alarmmanager is starting my service and somtimes not. It seems that if it has to wait for lets say more than 4 hours it is not working any more and my service is not called. I have set all neccessary permission otherwise it would not work at all. You can have a look at the code of the alarmmanager below:
someIntent = new Intent();
someIntent.setAction("START_SERVICE");
AlarmManager alarams ;
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, someIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarams = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+delay, alarmIntent);
The broadcast receiver is implemented like this (and it is registered!):
alarmReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(MainActivity.this, MyService.class);
startService(myIntent);
}
};
As I told you, if it is not working it stops before starting the service.
EDIT:
I have an idea. I read something in this thread: Android long running service with alarm manager and inner broadcast receiver
It might be, that my constructor for the intent "someIntent" only works for BroadcastReceivers declared in the manifest file. So in my case I should maybe use someIntent = new Intent("START_SERVICE") instead of someIntent = new Intent(); someIntent.setAction("START_SERVICE"). This effect is called tunneling - i will figure it out, if it works i will post my experience here. Thanks for the well explained answer on the mentioned thread! If you have any other ideas or the same experiences like me please let me know!
eMu
If the device is shutdown and start up then you will not get the alarm maanger broadcast receiver.
Implement OnBootReceiver which will receive the OnBoot completed and there you can start your pending alarms that were not fired.
I'm usign an Alarm Manager to update a widget with a Service. I've two different questions.
First question: I'm calling the service with Alarm Manager's intent. Like this:
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyService.class);
pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
context.startService(new Intent(context, MyService.class));
Long repeat = Long.parseLong(prefs.getString("update_preference", "600"));
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 1000*repeat, pi);
Is it wrong?
It looks and works right. But when I have looked at working services, I can't see my service name in the list. Perhaps it's not running as a single/seperate service. Just saw application name (not as a service). I'm not sure how to seperate or does it matter?
Another question: Over long time, running application, which controls widgets update, is closed somehow (manually or by a task killer). Of course Alarm Manager gonna stop and widget's functions gonna stop too. For example button clicking.
But, Twitter solved this problem. While the widget is active, if I close the main application (Twitter) -which controls widget- than click the widget, somehow widget triggering application and it starts again well. So buttons work properly. How is that possible?
Any help would be appreciated.
You dont need to do context.startservice that what the pending intent is for, if you want the service to run right away the first time just set it to run at the current time then set the interval from the current time.
You are also setting 2 different types of repeating when you don't need to setRepeating is strict where setInexact is not and can be adjusted by the OS when it gets fired hence the inexact in it. You want one or the other not both.
Also those intervals are very small and its going to kill the battery significantly.
It should just be this
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyService.class);
pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Long repeat = Long.parseLong(prefs.getString("update_preference", "600"));
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis(), 1000*repeat, pi);
It's good that your application/service isn't running all the time.
In fact it doesn't need/has to run all the time for updating a widget. If you schedule an Intent with AlarmManager the application will be started when the intent is fired if it has been closed.
Im creating a news-like app and I want to know is is possible to start app (ex : 1 time per 1h) to check if new data is available. You know what im talking about, I think. App should start from nowhere, check and finish().
Is this possible? I know that widgets can do it but normal activity or something like this?
Please, help.
Damian.
Yes, It is possible to use an "Alarm Service" in android, and use it to perform some work, at specific intervals.
Here is the link to Alarm Service documentation:
http://developer.android.com/reference/android/app/AlarmManager.html
Here is a sample code which uses Alarm Service
/**
* Method to start background service for server refresh and other tasks.
*/
public void startMyService()
{
//Start Service service to handle data refresh
Intent serviceIntent = new Intent(this, MyCommunicationService.class);
//Schedule additional service calls using alarm manager.
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(this, 0, serviceIntent, 0);
//Retrieve time interval from settings (a good practice to let users set the interval).
MyPreferenceManager prefManager = new MyPreferenceManager(this);
alarmManager.cancel(pi);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), prefManager.getDataRefreshTime()*1000 , pi);
}
Note that we are multiplying with 1000, because the parameter for setRepeating() method is in milliseconds.
When setting a service to go off at particular time, I use the AlarmManager system service.
Everything goes off without a problem, service is called and actions take place.
When the alarm time is reached, the service starts, and at this point I get the system time (System.currentTimeMillis()). I'm guessing this wont be the actual time the service start. Is there a way to get the time that was set for this PendingIntent?
ie
Set alarm for 9am.
DoStuffService starts at 9am.
DoStuffService knows it was supposed to start at 9am, and uses this value for future functions.
When you create an intent for your alarm, you could put extra data, including time of the alarm, into it like this:
Intent intent = new Intent("action name");
//put extra data into the intent:
intent.putExtra("alarm_time_hours", hours);
intent.putExtra("alarm_time_minutes", minutes);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
Then in your receiver or service you need to get this extra data from received intent. Use something like this:
Bundle bundle = intent.getExtras();
if(bundle.containsKey("alarm_time_hours")) {
int hours = bundle.getInt("alarm_time_hours");
}
if(bundle.containsKey("alarm_time_minutes")) {
int minutes = bundle.getInt("alarm_time_minutes");
}
Is there a way to get the time that was set for this PendingIntent?
No, sorry.
However, it should not be terribly difficult for you to determine it yourself. Following your example, if your service reports that it is now 09:00:02.36, you should be able to round down to determine that this is the 9am alarm.