Send data to the server from an Android device when the application enters the background

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

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

What is the best way to achieve this. Asynctask is not a good answer because I want to send data periodically. Never. I already use asynctasks as a way to send data to the server, what I need is something that works along with activity but does not stop SO.

EDIT:

I got this error using the code below.

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 someone tell me what this error means? I do not have a class with this receiver, but although if I register it in the manifest, I will not need it.

+4
source share
1 answer

You can write servicesand AlarmManagerto do this. Just register your application in the services and call the method alarmMangaer.setRepeat()to run your serveride code or any other operation that you want to do in the onStart()services method

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 to 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);
        }
    }
}
+4
source

Source: https://habr.com/ru/post/1533357/


All Articles