I'm trying to write a service that will check every midnight for new data from the server and will download it.
But when i start the app the mainActivity screen reloads after few seconds. I'v checed it and it happens because of this service, Why is this happening?
Her are the files:
MainActivity: i'v created an AlarmManager object to set pendingIntent:
//Set alarm
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReciever.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 24 * 60 * 60;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
AlarmReciever:
public class AlarmReciever extends BroadcastReceiver {
private Data newData = null;
public SharedPreferences settings;
ConnectivityManager cm = null;
NetworkInfo netInfo = null;
@Override
public void onReceive(Context context, Intent intent) {
newData = new Data(context);
// TODO Auto-generated method stub
newData.cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
newData.netInfo = newData.cm.getActiveNetworkInfo();
newData.settings = PreferenceManager.getDefaultSharedPreferences(context);
// System.out.print("-----------------" + newData.netInfo);
newData.checkOnline();
}
}
Data.java:
public void checkOnline(){
if (isOnline()){
System.out.print("**************** YES Internet");
firstAsyncTask task = new firstAsyncTask(this);
try {
Object dobj = task.execute("par1", "par 2", "par 3").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}else{
System.out.print("**************** NO Internet");
}
}
The data.java file is to big to post in here, but it seems that the "checkOnline" method in in causing the app to reload the MainActivity page, should i send the service differently?
Thanx for reading & answering.
Looks like you have written this line by mistake
pendingIntent = PendingIntent.getService(getApplicationContext(), 0, alarmIntent, 0);
It should be like this
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
Because you are using BroadcastReciever.
Causes the following line network access?
If so then the system might kill your process (ether for networking on main thread or for event loop timeout aka. ANR). And eventually restart it again if its a service.
In your
Activity
you do this:This causes the
BroadcastReceiver
to be triggered immediately, since you have specifiedSystem.currentTimeMillis()
as the time to trigger the first time.You probably mean to use
calendar.getTimeInMillis()
as the first time to trigger the alarm. But even if you change it to that, it will still trigger immediately because you've set the time in your calendar to 00:00 of the current day, which has already passed! You need to either usecalendar.getTimeInMillis() + interval
(which would be 00:00 of the following day, or you can add 1 day to your calendar before usingcalendar.getTimeInMillis()
.