如何停止运行多个实例的服务?

时间:2018-01-08 15:35:04

标签: android

我正在开发一个Android应用程序,它将在4秒间隔后更新设备位置,并且根据从服务器收到的响应,它将打开特定活动。

问题1)在某些情况下,它会打开一个活动,如带声音的来电。当我从最近的应用程序中删除应用程序时,我遇到问题。我注意到poll函数同时运行了两次,并且多个媒体同时播放。

问题2)我正在使用IntentService的Service intead(我是初学者,不确定哪个更好)。即使手机进入睡眠模式,后台服务也应该运行,就像WhatsApp或其他信使运行一样。

由于文件足够大,我只附加重要部分

public class TaxiNorrService extends Service实现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;

@覆盖     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();
}
}

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。在onStartCommand中编写的代码应该在onCreate函数中。这是因为onCreate将在服务首次启动时执行,而onStartCommand将在每次启动应用程序时执行。请按照此主题, Android - running a method periodically using postDelayed() call