Application need synchronize data from server in interval (for example every 30min) or manually on background. Result is saved to local database. After sync is done I want remind activity/fragment ... and force update list (if needed). There are many activities, so I want move it outside of activity and make it more consistent.
For now I created AsyncTask which get result from server and save to DB.
What shoud I use? BroadcastReciever, Service, AlarmManager?
UPDATE
Based on answers I start alarm in Application
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setInexactRepeating(AlarmManager.RTC, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
Created Receiver
public class AlarmReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent syncIntent = new Intent();
syncIntent.setClass(context, DataSyncer.class);
startWakefulService(context, syncIntent);
}
}
Created IntentService
public class DataSyncer extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
// get data from server
// save to DB
AlarmReceiver.completeWakefulIntent(intent);
}
}
And registered Receiver and Service in AndroidManifest
<service
android:name="com.cloudit.tsystems.app.DataSyncer"
android:enabled="true">
</service>
<receiver
android:name="com.cloudit.tsystems.app.AlarmReceiver"
android:enabled="true">
</receiver>
Where and how I notify that sync is done in Activity/Fragment?
I would use the AlarmManager and register a BroadcastReceiver. Once the receiver is fired, I will launch an IntentService to download the data in the background.
Configure your alarm:
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setInexactRepeating(AlarmManager.RTC, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
Create a BroadcastReceiver that will get notified when the alarm goes off. Note that I'm using a WakefulBroadcastReceiver so that the device doesn't go to sleep when you're syncing.
class MyBroadcast extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent syncIntent = new Intent();
syncIntent.setClass(context, DataSyncer.class);
startWakefulService(context, syncIntent);
}
}
Next, an IntentService that will download data in the background:
class DataSyncer extends IntentService{
...
#Override
protected void onHandleIntent(Intent intent) {
//sync data
MyBroadcast.completeWakefulIntent(intent);
}
}
Update:
So now that you have your data synced, there are several options to notify Activities and Fragments. You can use a LocalBroadcastManager to broadcast. Take a look at this link for more details.
Use an AlarmManager to trigger a PendingIntent on a 30 minute interval that starts an IntentService to do your download.
Intent intent = new Intent(context, PollingService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(
AlarmManager.RTC,
System.currentTimeMillis(),
AlarmManager.INTERVAL_HALF_HOUR,
pendingIntent
);
When the IntentService is done updating your data, it can send a Broadcast that your Activity/Fragment has registered to listen for to notify it of new data and refresh it's view.
sendBroadcast(new Intent("DATA_UPDATED"));
In your Fragment
getActivity().registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//update UI
}
}, new IntentFilter("DATA_UPDATED"));
Related
I am new to android programming. What am i doing wrong? AlarmReceiver class not triggering...I want to trigger repeatedly the AlarmReceiver.class ...
I used the code from this page https://developer.android.com/training/scheduling/alarms#precision
OnCreate at MainActivity.java i have the following code
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
//Check if the PendingIntent already exists
PendingIntent pendingIntent =
PendingIntent.getService(this, 6661, intent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null && alarmManager != null) {
//cancel here if you want
}
//Run every 60 seconds!
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000,
60000, pendingIntent);
The AlarmReceiver.java class includes the following code...
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Log.i("logit","testing...");
}
}
Do you have an entry in your manifest for the receiver like that?
<receiver android:name=".AlarmReceiver"></receiver>
I am developing one android application, In which i need to call onReceive method of Alarmmanager from onReceive of another BroadcastReceiver i.e. Internet connectivity. Is it possible ? Or should i duplicate all my stuff in another BroadcastReceiver?
You can make a new intent from onReceive to trigger another broadcast receiver
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent("com.domain.yourboardcastreceiver");
context.sendBroadcast(newIntent);
}
In OnReceive(..) method of Internet Connectivity broadcast receiver, you can set alarm and thats how alarm manager will get triggered, eg :
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(getBaseContext(), **AlarmReceiver**.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
Don't forget to register alarmmanager's receiver in your manifest file.
Hope it helps !
I would like to setup an IntentService to fire on a timer and manually. In the case that new data is created I would like to 'manually' trigger the IntentService to send the data off immediately. The timer will just run every few minutes to ensure all data has been sent off, if it has not it will send data.
Is there a way to block/lock the IntentService such that if I am already sending data manually and the timer fires the service does not try and submit the same data twice?
I have the following IntentService which will pull an item out of the database and post that info to a server.
public class MyIntentService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
Long id = intent.getLongExtra(SERVICE_ID, -1);
// grab stuff out of DB based on id and submit to server
}
}
I have a BroadcastReceiver that fires every so often to start that intent service:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//Grab stuff I care about
Long someId = ....;
Intent i = new Intent(context, MyIntentService.class);
i.putExtra(SERVICE_ID, someId);
context.startService(i);
}
}
And I start the broadcast receiver, to fire every 10 minutes
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 60 * 10, pendingIntent);
I fire manually elsewhere in code based on an event when I have new data to send
// On some event happened manually start intent (not on timer)
Context context = getApplicationContext();
Intent intent = new Intent(context, MyIntentService.class);
intent.putExtra(SERVICE_ID, someId);
context.startService(intent);
Again can I block somehow to ensure there are not multiple MyIntentServices running at the same time, which could be possibly sending the same data?
In my app, I want to send sensor data every ten seconds via POST to a webserver.
I am doing this with an activity who starts/stopps a AlarmMananger who is calling an IntentService.
Problem is: the target URL is generated in the Activity and doesn't arrive at the IntentService.
Activity:
public class MyActivity extends AppCompatActivity {
...
#Override
protected void onResume() {
super.onResume();
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
String targetURL = "www.google.de";
alarmIntent.putExtra("targetURL", targetURL);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pendingIntent);
}
...
}
Broadcast-Receiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String targetURL = intent.getStringExtra("targetURL");
Intent newintent = new Intent(context, SendPostRequest_Service.class);
newintent.putExtra("targetURL", targetURL);
context.startService(newintent);
}
}
Intent Service:
public class SendPostRequest_Service extends IntentService implements SensorEventListener{
...
public SendPostRequest_Service() {
super(SendPostRequest_Service.class.getName());
}
#Override
protected void onHandleIntent(Intent intent) {
String targetURL = intent.getStringExtra("targetURL");
// Problem: targetURL = null
//read sensors, send POST-Request via okhttp <- working
}
...
}
Do you have any suggestions?
Put your code into a test project. Only way I was able to get it working was to change the order of when the extra is added to the intent. Update MyActivty with the below. It would appear to G. Blake Meike point that there's a copy of the intent being done.
#Override
protected void onResume() {
super.onResume();
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
String targetURL = "www.google.de";
alarmIntent.putExtra("targetURL", targetURL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pendingIntent);
}
You'll need to take a wake lock in your BroadcastReceiver and release it in your Service. Otherwise, the system may return to a sleep state before your Service gets a chance to run. The WakefulReceiver is particularly useful for this. If you'd like some more details, this article will help: http://hiqes.com/android-alarm-ins-outs/
I need to run a service in time interval, for example every 2 minutes. I register it with AlarmManager, it works fine when the service stops itself before that 2 minutes is up, but there is a great chance it will take more than 2 minutes, in this case I'll need the service to be terminated and start up a new one, how can I do this?
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), Sender.class);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 30, pi);
Instead of starting service by AlarmManager use broadcast. Set AlarmManager to send some broadcast intent. Create your own BroadcastReceiver that will receive that intent and in onReceive method restart(stop and start) service.
//Start AlarmManager sending broadcast
Intent intent = new Intent(context, MyBroadcastReceiver.class); // explicit
peningIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30 * 1000, pendingIntent);
.
//BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(SynchronizationService.class.getName());
context.stopService(serviceIntent);
context.startService(serviceIntent);
}
}
.
//Register receiver in AndroidManifest.xml in Application tag
<receiver
android:name="com.example.MyBroadcastReceiver" >
</receiver>
You should write a login in onStartCommand itself.
Check if service is running or not using variable.
If its running call stopSelf method on service,then again call startservice for same service.
start a alarm manager services u have to use this code
Intent intent = new Intent(activity.this,Sender.class);
pendingIntent = PendingIntent.getBroadcast(activity.this.getApplicationContext(),1, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 30, pendingIntent);
for stop this broadcast receiver use this code
Intent intent = new Intent(activity.this,Sender.class);
pendingIntent = PendingIntent.getBroadcast(activity.this.getApplicationContext(), 1,intent, 0);
pendingIntent.cancel() ;