2017-04-08 1 views
1

5 초 후에 함수가 반복적으로 호출되는 서비스를 실행하는 응용 프로그램을 만듭니다. 버튼을 클릭하여 서비스를 시작할 수 있지만 다른 버튼을 클릭하면 멈추지 않습니다!서비스가 중지되지 않습니다.

내 코드는 다음과 같습니다

public class LocationService extends Service implements LocationListener { 
    public static final String MyPREFERENCES = "MyPrefs" ; 
    public static final String TAG = "MyServiceTag"; 
    Timer t; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 
    @Override 
    public void onDestroy(){ 
     super.onDestroy(); 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     t = new Timer(); 
     t.scheduleAtFixedRate(
       new TimerTask() 
       { 
        public void run() 
        { 
         startJob(); 
        } 
       }, 
       0,  // run first occurrence immediatetly 
       5000); // run every 60 seconds 
     return START_STICKY; 
    } 

    @Override 
    public boolean stopService(Intent name) { 
     // TODO Auto-generated method stub 
     t.cancel(); 
     t.cancel(); 
     return super.stopService(name); 

    } 

시작 및 정지는 다른 활동에서 이루어집니다.

public void popupstart() { 
     android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(this); 
     alertDialog.setTitle("Enable Location Sharing"); 
     alertDialog.setMessage("Enable location sharing will broadcast your location to clients applications. This is a battery draining process and kindly turn off " + 
       "location sharing after use"); 
     alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       SharedPreferences.Editor editor = sharedpreferences.edit(); 
       editor.putString("shareLocation", "yes"); 
       editor.commit(); 
       liveStatus = "1"; 
       mFab = (FloatingActionButton)findViewById(R.id.fab); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
        mFab.setImageDrawable(ContextCompat.getDrawable(gnavigationActivity.this, R.drawable.locationon)); 
       } 
       else{ 
        mFab.setImageDrawable(getResources().getDrawable(R.drawable.locationon)); 
       } 
       if(!isMyServiceRunning(LocationService.class)) { 
        Toast.makeText(gnavigationActivity.this, "Location Sharing started", Toast.LENGTH_LONG).show(); 
        processStartService(LocationService.TAG); 
       } 
       else{ 
        Toast.makeText(gnavigationActivity.this, "Location Sharing already started", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
     alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 
     alertDialog.show(); 
    } 





public void popupstop() { 
     android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(this); 
     alertDialog.setTitle("Stop Location Sharing"); 
     alertDialog.setMessage("You are about to stop location sharing which now will not broadcast location to client users. Are you sure?"); 
     alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       processStopService(LocationService.TAG); 
       Toast.makeText(gnavigationActivity.this, "Location Sharing Stoped", Toast.LENGTH_LONG).show(); 
       SharedPreferences.Editor editor = sharedpreferences.edit(); 
       editor.putString("shareLocation", "no"); 
       editor.commit(); 
       liveStatus = "0"; 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
        mFab.setImageDrawable(ContextCompat.getDrawable(gnavigationActivity.this, R.drawable.locationoff)); 
       } 
       else{ 
        mFab.setImageDrawable(getResources().getDrawable(R.drawable.locationoff)); 
       } 
      } 
     }); 
     alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 
     alertDialog.show(); 
    } 

private void processStartService(final String tag) { 
     Intent intent = new Intent(getApplicationContext(), LocationService.class); 
     intent.addCategory(tag); 
     startService(intent); 
    } 
    private void processStopService(final String tag) { 
     Intent intent = new Intent(getApplicationContext(), LocationService.class); 
     intent.addCategory(tag); 
     stopService(intent); 
    } 

답변

1

stopService (intent);

재정의 방법은 들의 OnDestroy

@Override 
    public void onDestroy(){ 
     super.onDestroy(); 
     t.cancel(); 
     t.cancel(); 
    } 
을 할 수

시도를 시작합니다