General regarding service class and multithreading - android

I am running a code, where the user selects a date and time. The user can select any date and time in the future. These dates and time are stored in sqlite database. After the user selects those dates and time, the activity calls a service class, where I am running a new thread in the following way
public int onStartCommand(Intent intent, int flags, int startId) {
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final SqliteController db = new SqliteController(getApplicationContext());
new Thread(new Runnable() {
#Override
public void run() {
List<Greetings> greetings = db.getAllGreetings();
if (db.getGreetingsCount() >= 0) {
do {
for (Greetings g : greetings) {
Calendar cal = Calendar.getInstance();
.........
.........//other codes
This thread access the data from the database and matches the time and date with the system time and date. One the date and time matches, I am using alarm manager with broadcast receiver like this
if (dnt.equals(cdt)) {
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
aint.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
db.deleteGreetings(g);
}
I wanted to know, Is this the correct way to do it? When I run the program in emulator, sometimes it runs fine, but other times it shows "Application doing too much work in the main thread". So, am I doing something wrong? or is there a better way to do it?

Since nearly all of your posted code is running in a background thread, this would not cause your application to be doing too much work in the main thread. Your problem is likely coming from another source.
You could try profiling your app using traceview as described in the Android documentation here. Try to find which methods are consuming the most time to narrow down your search.
As an aside, you should use an IntentService instead of a Service if your service is just creating a single thread running this task in the background. You then implement the onHandleIntent method instead of the onStartCommand method. The IntentService will handle all operations it receives in a single background Looper.

Related

Android service setInexactRepeating asyncTask issue with SharedPreferences

My application for students downloads marks in a background service and checks if there are more marks than last time. If there are any new marks, it pushes notification and rewrites the number of marks in sharedPreferences. It works, but sometimes it creates multiple notifications for the same mark at the same time.
The IntentService which triggers a broadcast receiver:
public int onStartCommand(Intent intent, int flags, int startId) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), CheckNewMarksReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
alarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
0,
2500, alarmIntent);
return START_STICKY;
}
The broadcast receiver onReceive method initializes sharedPreferences attribute and numberOfMarksSaved and starts AsyncTask that downloads data:
DownloadDataTask task = new DownloadDataTask();
task.execute(context.getString(R.string.LOGIN_PAGE_URL), username, password);
dataPreferences = context.getApplicationContext().getSharedPreferences(context.getString(R.string.SHARED_PREFERENCES), Context.MODE_PRIVATE);
numberOfMarksSaved = dataPreferences.getInt(context.getString(R.string.PREFERENCE_NUMBER_OF_MARKS_KEY), -1);
AsyncTasks onPostExecute method checks if the number of downloaded marks is greater than last time:
if (numberOfDownloadedMarks > numberOfMarksSaved) {
...
notifyUserAbout(newMarks, response); // method to fire a notification
}
dataPreferences
.edit()
.putInt(context.getString(R.string.PREFERENCE_NUMBER_OF_MARKS_KEY),
numberOfDownloadedMarks)
.apply();
As I said, this mostly works, but sometimes the BroadcastReceiver is triggered twice in a row immediately - the first one rewrites the value in sharedPreferences, but the second one doesn't "notice" that value in shared preferences was changed. How can I prevent that?
EDIT:
I tried even setRepeating instead of setInexactRepeating - nothing changed (I suspect android alarmManager time shifting). Here is my Log:
06-11 18:14:27.732 ... I/Just about to: download new data
06-11 18:14:27.761 ... I/Just about to: download new data
06-11 18:14:27.907 ... I/Saved & new: 89, 90
06-11 18:14:27.933 ... I/Notification baked - id: 1077819997
06-11 18:14:28.004 ... I/Saved & new: 89, 90
06-11 18:14:28.006 ... I/Notification baked - id: 1077820069
Because of how inexact repeating alarms work and the long running background operations, most probably there are multiple AsyncTask instances running at the same time when the problem occurs.
From the docs of AlarmManager:
Your alarm's first trigger will not be before the requested time, but
it might not occur for almost a full interval after that time. In
addition, while the overall period of the repeating alarm will be as
requested, the time between any two successive firings of the alarm
may vary.
For such low intervals i'd suggest to use setRepeating() instead of setInexactRepeating()
and
keep an instance of your AsyncTask in your Service.
Then, you could do something like this:
if(task.getStatus == AsyncTask.Status.FINISHED) {
task = new DownloadDataTask();
task.execute(context.getString(R.string.LOGIN_PAGE_URL), username, password);
}
To prevent multiple instances of your AsyncTask running at the same time.

Android execute a function after 1 hour

I have an android application where I am storing user's data on database when he/she activates the app. My app requires the user to stop the application manually in order to remove its entry from the database and along with that other services which keep running when the app is activated.
So I want to write a function which will be executed after every hour (when the app is activated) and will give a notification to user just to remind him/her about the service which is running .If the user had forgot to stop the service then they can stop it or continue with service.
What is the best efficient way of doing this. I dont want to drain too much of battery with thihs 1 hour basis check if the user considers it to run for a day or so. Please advice. Thanks :)
I suggest the code will be like this.
// the scheduler
protected FunctionEveryHour scheduler;
// method to schedule your actions
private void scheduleEveryOneHour(){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(WAKE_UP_AFTER_ONE_HOUR),
PendingIntent.FLAG_UPDATE_CURRENT);
// wake up time every 1 hour
Calendar wakeUpTime = Calendar.getInstance();
wakeUpTime.add(Calendar.SECOND, 60 * 60);
AlarmManager aMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
aMgr.set(AlarmManager.RTC_WAKEUP,
wakeUpTime.getTimeInMillis(),
pendingIntent);
}
//put this in the creation of service or if service is running long operations put this in onStartCommand
scheduler = new FunctionEveryHour();
registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_ONE_HOUR));
// broadcastreceiver to handle your work
class FunctionEveryHour extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// if phone is lock use PowerManager to acquire lock
// your code to handle operations every one hour...
// after that call again your method to schedule again
// if you have boolean if the user doesnt want to continue
// create a Preference or store it and retrieve it here like
boolean mContinue = getUserPreference(USER_CONTINUE_OR_NOT);//
if(mContinue){
scheduleEveryOneHour();
}
}
}
hope that helps :)
Use AlarmManager refer this and tutorial with PendingIntent
Try this way,hope this will help you to solve your problem.
new Timer().scheduleAtFixedRate(task, after, interval);

Android Automatic Task in a specific time

I am new in Android development. I am developing a twitter client. I would like to send a tweet at every morning at 8. I want to set the schedule like Alarm. How can I do that ? I will be happy if you point me to some examples or other resources. Thanks.
Here is an example of the AlarmManager:
private void daylyTask()
{
daylyBR = new BroadcastReceiver() {
#Override public void onReceive( Context context, Intent _ )
{
//Do something
Log.d(TAG, "daylyTask uitgevoerd.");
}
};
getApplicationContext().registerReceiver( daylyBR, new IntentFilter("yourApp.blah") );
daylyPendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, new Intent("yourApp.blah"), 0 );
GregorianCalendar cal = new GregorianCalendar();
cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
cal.set(GregorianCalendar.MINUTE, 0);
// set alarm to fire 5 sec (1000*5) from cal repeating every 86400000L ms (1 day)
manager.setRepeating( AlarmManager. RTC_WAKEUP, cal.getTimeInMillis() + 5000L, 86400000L, daylyPendingIntent );
}
Check out AlarmManager.
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
Look up the AlarmManager API. I have used it to do things like what you are describing.
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
http://developer.android.com/reference/android/app/AlarmManager.html

Android: Setting up a periodical alarm with AlarmManager

In my app, I need to add a row to a database and simultaniously set up an Alarm event to repeat every day at the time specified in one of the database columns. I already have some code, but it doesent trigger the alarm event at the specified time. Here is my code:
public class Add_reminder extends Activity {
AlarmManager am;
int hours, minutes;
REMIND_DB db;
Calendar calendar;
Cursor cursor;
Button button;
public void onCreate(Bundle savedInstanceState) {
//The usual code in the beginning of onCreate
//I load db from extended Application class as global since i use it in more
//Activities. Ints hours and minutes is set by user interaction
calendar = Calendar.getInstance();
am = (AlarmManager) getSystemService(ALARM_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.open();
db.insertReminder( -- parameters for database --);
cursor = db.getAllReminders();
cursor.moveToLast();
calendar.set(Calendar.HOUR, hours);
calendar.set(Calendar.MINUTE, minutes);
Intent intent = new Intent(Add_reminder.this, ReminderAlarm.class);
intent.putExtra("id_of_db_row", cursor.getInt(0));
PendingIntent pi = PendingIntent.getActivity(Add_reminder.this,
cursor.getInt(0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*3600*1000, pi);
db.close()
}
});
}
}
Database is updated correctly, but the ReminderActivity never starts on specified time. I dont know what could be wrong. I saw some example codes using BroadcastReceiver instead of starting the Activity right on with the PendingIntent, but this should work too, right? Does anyone knows what could be wrong?
My second question is if Im going to need the same instance of AlarmManager when I want to add or remove some alarms from a different Activity, or do I just declare another AlarmManager in every Activity I need?
Thank you!
You should use a broadcast receiver for alarms, and then start a service that does the actual work. Broadcast receivers shouldn't block the UI thread with lengthy operations (such as writing to the DB). Additionally, 'once a day' alarms might be problematic: if the user reboots the phone: registered alarms will be lost. You need to:
save the time the alarm is supposed to run to, say, SharedPreferecnes
re-register your alarm when the phone boots (receive the BOOT_COMPLETED broadcast)
don't use setRepeating() but let each alarm register the next on
Using a shorter period (1 or 2 minutes) for testing also helps.
As for the AlarmManager instance, it's a system service, you don't need to care about what instance you are using. Just get it using getSystemService()

Best way to set a reoccurring android task

I am creating the following thread in my onCreate but realized that this call needs to execute every 20 minutes or so:
Thread t = new Thread() {
public void run(){
setTopUsers();
}
};
t.start();
private void setTopUsers() {
...
for(Map.Entry<Double,String> entry : myMap.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if(...)
view.setText(...)
}
}
The method that's being called simply processes information from a TreeMap and displays the results to the UI. I'm trying to figure out what is the best way to handle this: 1) Create a Timer that runs on a separate thread (but I've read that's not a good idea), 2) Create a service Intent that processes every 20 minutes (seems like overkill).
Any suggestions?
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29
Something like this should work:
Intent intent = new Intent(this, ProcessService.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.cancel(pIntent);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), INTERVAL, pIntent);
Let the user decide. Implement both, there's nothing worse than a repeating process the user can't control.

Categories

Resources