Android - Having a service run every day at 4AM - android

I would like to know the best practices for running a Service every day at 4AM.
The way I think I should be doing it is to create a new repeating alarm using AlarmManager and having it run the service at 4AM. Problem is, I'm not sure where to put the code to set the alarm.
Do I do it in my main activity as one of the first tasks in the OnCreate method? Do I do some funky stuff with BroadcastReceivers and intents? What happens when a user updates my app? What happens when a user restarts?
Any help with these questions would be much appreciated :) Sample code would be helpful as well!
Bara

You can schedule your alarm each time phone boots and each time your application starts. To listen to phone boot event you can use BroadcastReceiver.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
.
.
.
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
For a complete sample you can take a look at Photostream application http://code.google.com/p/apps-for-android. It uses exactly the same approach.

Related

Developing an app which works as a DND application and blocks all the notifications for given time

I am a beginner to android and I want to develop an application which blocks all the other app notifications for some period of time. Is it possible to block other app notifications ? If it is possible, can someone guide me for that? Can anyone please help me with this?
I have used one approach which is the method of InterruptionFilter but that doesn't work. Below is my code where I have used it.
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//On button click calling the below method
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
NotificationListenerService is the best fit for this.
A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.
To extend this class, you must declare the service in your manifest file with the Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action. For example:
<service android:name=".NotificationListener"
android:label="#string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Documentation : NotificationListnerService
Probably You need to implement the methods like cancelAllNotifications() , cancelNotification(String key) to block other app notifications.

Receive alarm in spite of that device has been restarted

I want to make an alarm clock. I've done it but it works only if device wasn't restarted.If it was I just don't get any alarm. What should I do to receive my alarm no matter has device been restarted or not?
That's how I set alarm time:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pi1);
This is what I tried in a similar situation:
Make your broadcast receiver detect the system restart. Now, as soon as your system restarts, your broadcast receiver will detect it and you can perform your task. Like, in my project, I ran a query onto my SQLite database and retrieved all the alarms that have not executed and then executed them according to my requirements and updated the database with the same.
You can try something similar to it.
Code to retrieve alarms:
Add your Broadcast Receiver with the following attributes:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This BroadcastReceiver will detect your Android device's restart and execute the onReceive() method of the BroadcastReceiver. In the onReceive() method, retrieve all the alarms according to your criteria and then process them further.
To learn further about how to retrieve records or tuples from the SQLite Database go through the following link:
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow

Schedule AlarmManager Android

Can I schedule alarm manager (.set() methode) in android to a time which is one month later the current time
will it be alive for that mutch of time ?
what will be the OS effects on this alarmManager ?
Tip for a case when user reboots his phone. You can declare BroadcastReceiver with the following intent-filter:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
and re-schedule your alarm in onReceive(...) of your receiver class. Intent will be delivered to the receiver after user reboots his phone. Don't forget to add the following permission to your manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Or you can use the library that will handle such cases for you:
https://github.com/commonsguy/cwac-wakeful
Theoretically yes, If the user keeps his phone on for that long, however the alarm doesn't survive reboots and will therefore not be scheduled when the phone restarts.

I'm trying to figure out how to run a thread or activity in the background but still allow user's to use other app's

I'm trying to make an app for work that acts as parental control's and control's the wifi state, gps state, mobile state, and 4G state. I need it to run in the background to check the state's every 60 sec and then change it if needed. Also it needs to start doing this as soon as the device turns on. My question is how do I run it in the background but still allow the user to use one other app? (yes i know this would most likly drain battery life.)
The thing what you are looking for is called Services.
They offer the functionality which you are looking for. You can also start a service with the phone bootup and interact with them via IPC.
Register a BroadcastReceiver to start your application just after the device turns on
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver
android:name="com.package.YourReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
In your BroadcastReceiver, do whatever initialization you need and then configure the AlarmManager to be notified periodically, ideally on a WakefulBroadcastReceiver
Every time you are notified, start a IntentService and do your work on there.

Broadcast Receiver not working after reboot

I have an broadcast receiver registered in the manifest for intercepting calls, like this:
<receiver android:name="CallTracker">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Everything works fine and the receiver gets called.
But after I restart the device or my AVD, the receiver doesn't get called anymore.
What do I have to do in order to get my broadcast receiver to work after reboot, too?
Do I need PackageManager for that?
I like the registration in the manifest, since the application doesn't have to be active in order for the app to intercept the call.
I know that this question is quite old but in my opinion still relevant. Since the currently accepted answer is wrong and the comment by user1806772 was the correct answer for me, I provide it as a new answer to the question:
It probably does work. But directly after reboot it can take a really long time (up to multiple minutes) until the intent is delivered. Some minutes after the reboot it should work fast again.
You need to add this action to your intent-filter.
<action android:name="android.intent.action.BOOT_COMPLETED" />

Categories

Resources