Starting the boot process in the background, even when the phone is sleeping

So, I developed an Android application that determines the user's location every 5 seconds using a smooth location (LocationClient), and send this data to my server. This whole process is repeated every 5 seconds when the application is running. I am loading data in the background using the AsyncTask class.

  • Problem:

    Data download stops when the user closes the application or when the phone goes into standby mode.

  • What I want:

    I want the application to constantly send location data to my server, even when the user closes the application or when the phone is sleeping. This process has to be run in a separate thread, since I do not want this process to make my user interface thread immune.

  • What I have found so far:

    I have heard about services, intentservices and alarmmanager, but I do not know which one to use. I also heard about wakelocks to keep the processor awake. Remember that I do not want to keep the screen on all the time, as this will drain the battery.

How can I make the application send data to the server all the time?

+3
source share
2 answers

Service AlarmManager 5/10 ... MainActivity

public static AlarmManager alarm;
    public static PendingIntent pintent;

  // write this code on button click

            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 10);

            Intent intent = new Intent(this, MyService.class);


            pintent = PendingIntent.getService(this, 0, intent, 0);


            alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);

 // button click functionality over


    // write this code outside onCreate()
    protected ServiceConnection mConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub

            }
        };

MyService

public class MyService extends Service {
    public static int counter = 0;


    public MyService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new  Binder() ;
    }
    @Override
    public void onCreate() {
        Toast.makeText(this, "First Service was Created", Toast.LENGTH_SHORT).show();
       }

    @Override
    public void onStart(Intent intent, int startId) {

        counter++;
        Toast.makeText(this, " First Service Started" + "  " + counter,               Toast.LENGTH_SHORT).show();



    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        }

    public void onTaskRemoved (Intent rootIntent){

        MainActivity.alarm.cancel(MainActivity.pintent);
        this.stopSelf();
       }

 <application
        ....
        <activity
         .....
          </activity>
     <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
      </application>
+5

, . , ( onDestroy ).

0

All Articles