4

Currently i can only send the data to the server when my app (activity) is in foreground. This happens at least in 4.1.3 because the android SO pauses the app or stops it.

I need to send data all the time even if the activity is in background.

What is the best way of achieving this. Asynctask is not a good answer because i want to send data periodically. Not once. I already use asynctasks as a way to send the data to the server what i need is something that runs together with the activity but doesn't get stopped by the SO.

EDIT:

I got this error using the below code.

04-03 13:55:28.804: E/AndroidRuntime(1165): java.lang.RuntimeException: Unable to instantiate receiver main.inSituApp.BootCompletedIntentReceiver: java.lang.ClassNotFoundException: main.inSituApp.BootCompletedIntentReceiver

Can anyone tell me what that error means? i dont have a class with that receiver but i though if i register it in the manifest i wouldn't need it.

  • 1
    Use a service: http://developer.android.com/guide/components/services.html – Ken Wolf Mar 25 '14 at 14:30
  • 1
    Use intent service or service ... http://developer.android.com/reference/android/app/IntentService.html http://developer.android.com/reference/android/app/Service.html – Ahmed Zayed Mar 25 '14 at 14:31

1 Answers1

5

You can write services and AlarmManager to do so. Simply register your application in services and call alarmMangaer.setRepeat() method to start your code of serverside or any other operation you want to do in onStart() method of services

public class MyService extends Service{
  Calendar cur_cal = Calendar.getInstance();
  @Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Intent intent = new Intent(this, MyService.class);
    PendingIntent pintent = PendingIntent.getService(getApplicationContext(),
            0, intent, 0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            cur_cal.setTimeInMillis(System.currentTimeMillis());
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),
            60 * 1000*3, pintent);
}
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
          // your code for background process
  }
}

Add this in AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
        android:name="com.yourpackage.MyService"
        android:enabled="true" />
  <receiver android:name=".BootCompletedIntentReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Edit: BootCompletedIntentReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootCompletedIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, MyService.class);
            context.startService(pushIntent);
        }
    }
}
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49