0

I am developing a android app, which will update device location after 4 seconds interval and depending on the response received from the server it will open specific activity.

Problem 1) In some case it will open up a activity like incoming phone call with sound. I am facing problem when I am removing the app from recent app. I noticed the poll function is running twice at the same time, and multiple media is playing at the same time.

Problem 2) I am using Service intead of IntentService(I am a beginner and not sure which will be better). The background service should run even the phone goes to sleep mode, just like WhatsApp or other messenger run.

As the file is big enough, I am attaching only important part

public class TaxiNorrService extends Service implements LocationListener {

...
...

final Handler poll_handler = new Handler();
private NotificationManager mNM;
private final Actions actions = new Actions();
public Ringtone r;
private String newtext;
private Runnable BreakRunnable;
private  Runnable poll_runnable;
private Handler BreakHandler;

@Override public int onStartCommand(Intent intent, int flags, int startId) {

poll_runnable = new Runnable() {
        @Override
        public void run() {
            if(!App.isAutoBreak()){

                if(BreakHandler !=null){
                    BreakHandler.removeCallbacks(BreakRunnable);
                }

                if(r != null) {
                    if (r.isPlaying()) {
                        r.stop();
                    }
                }
            }
            if (actions.checkPermission(getApplicationContext())) {
                checkGPS();
                if(isNetworkAvailable()){
                    if(App.isPollOn()){

                        poll(latitude, longitude);
                    }
                }else{
                    if(BreakHandler !=null){
                        BreakHandler.removeCallbacks(BreakRunnable);
                    }
                    boolean foregroud = false;
                    try {
                        foregroud = new ForegroundCheckTask().execute(getApplication()).get();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                    boolean background = isMyServiceRunning(TaxiNorrService.class);
                    if(foregroud == true && background == true && App.isAppForground()){
                        if(!App.isLoadingVisible()){
                            Intent intent = new Intent(TaxiNorrService.this, Loading_activity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                }
            }
            poll_handler.postDelayed(this, 4000);
        }
    }; 

    return Service.START_STICKY;
}

private void poll(double lat, double lon){
    //Connected to API endpoint
}

...
...

@Override
public void onDestroy() {
    if(r != null) {
        if (r.isPlaying()) {
            r.stop();
        }
    }

    poll_handler.removeCallbacks(poll_runnable);
    super.onDestroy();
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3783411
  • 37
  • 1
  • 7

1 Answers1

1

I found the answer for my questions. The code written in the onStartCommand should be within onCreate function. This is because onCreate will execute when service starts first time, and onStartCommand will execute every time when you start the app. Please follow this topic, Android - running a method periodically using postDelayed() call

user3783411
  • 37
  • 1
  • 7