androidweb-servicesservicebindservice

send data to server in background on every x sec or minutes


I need to send data to server continuously (say after every x seconds or min) even if app is in background.i know service is the best option for this.as i have never tried such scenario is there any handy example on how to make web-service call from service and then after deliver response to activity (whether data successfully or not).any approach or other way on how to achieve this. any help is appreciated.


Solution

  • First add this in mainactivity:

    AlarmManager alarmManager;
    PendingIntent pendingIntent;
    Calendar calendar;
    Intent alarm;
    final int SDK_INT = Build.VERSION.SDK_INT;
    
        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
    
    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm = new Intent(this, TestForecastService.class);
    pendingIntent = PendingIntent.getService(this, 0, alarm, 0);
    
    if (SDK_INT < Build.VERSION_CODES.KITKAT) {
        alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000, pendingIntent);
        Log.d("lowerMF","hahah");
    }
    else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent);
        Log.d("kitkatMF","hahah");
    }
    else if (SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent);
        Log.d("marshmallowMF","hahah");
    }
    

    Now, TestForecastService: in which you can define your work:

    public class TestForecastService extends IntentService {
    
        Context context;
        PowerManager powerManager;
        PowerManager.WakeLock wakeLock;
        final int SDK_INT = Build.VERSION.SDK_INT;
        AlarmManager alarmManager;
        PendingIntent pendingIntent;
        Intent alarm;
    
        public TestForecastService() {
            super("");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FCFCFCFC");
    
            wakeLock.acquire();
            sendDATA(city);
    
        }
    
        private void sendDATA(String city) {
            try {
    
               ......DO YOUR DATA SENDING WORK.........
    
            } catch (Exception e) {
                Log.v("fserviceerror","erre");
            }
    
            reSETALARAM();
            wakeLock.release();
        }
    
        private void reSETALARAM() {
            alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarm = new Intent(this,TestForecastService.class);
            pendingIntent = PendingIntent.getService(this, 0, alarm, 0);
    
            if (SDK_INT < Build.VERSION_CODES.KITKAT) {
                alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000, pendingIntent);
                Log.d("lowerFS","hahah");
            }
            else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent);
                Log.d("kitkatFS","hahah");
            }
            else if (SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent);
                Log.d("marshmallowFS","hahah");
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    }
    

    Now, make BrodcastReceiver class that run your service when device restarted:

    public class Auto extends BroadcastReceiver {
    
            final int SDK_INT = Build.VERSION.SDK_INT;
    
            @Override
            public void onReceive(Context ctx, Intent intent) {
    
                    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
                            AlarmManager alarmMgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
                            Intent intent1 = new Intent(ctx, TestForecastService.class);
                            PendingIntent alarmIntent = PendingIntent.getService(ctx, 0, intent1, 0);
    
                            if (SDK_INT < Build.VERSION_CODES.KITKAT) {
                                    alarmMgr.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000, alarmIntent);
                                    Log.d("lowerFB","hahah");
                            }
                            else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
                                    alarmMgr.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,alarmIntent);
                                    Log.d("kitkatFB","hahah");
                            }
                            else if (SDK_INT >= Build.VERSION_CODES.M) {
                                    alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,alarmIntent);
                                    Log.d("marshmallowFB","hahah");
                            }
                    }
            }
        }
    

    Finally, add this in Manifest file:

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
    
    <service android:name=".TestForecastService" android:exported="true" android:enabled="true"/>
    <receiver android:name=".Auto" android:exported="true" android:enabled="true" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>