2015-08-29 36 views
0

我想從我的MainActivityTrackingService類通過上下文來檢查onDestroy()裏面的方法isFinishing()在TrackingService但我正在逐漸The method isFinishing() is undefined for the type Context獲取isFinishing服務

我怎樣才能得到isFinishing()內工作的內部工作服務?

MainActivity

public class MainActivity extends ActionBarActivity implements 
     AsyncTaskCallback { 
    private static Context mContext; 

    public static Context getContext() { 
      return mContext; 
     } 

} 

TrackingService:

public class TrackingService extends Service implements AsyncTaskCallback, 
     LocationListener { 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(lm != null && MainActivity.getContext().){ 
      lm.removeUpdates(this); 
      System.out.println("ABC TrackingService lm was removed."); 
     }else{ 
      System.out.println("ABC TrackingService lm locationManager is null."); 

     }  
    } 

} 

編輯

我想,當應用程序被關閉刪除我TrackingService類的locationManger和不當用戶切換活動

有沒有辦法在onDestroy()被調用時區分兩種情況?

我有一個trackingService locationManager組件,它在後臺處理並從MainActivity活動啓動。同時,我有另一個組件從服務器檢索這些數據,並在Map活動中顯示它。當用戶點擊MainActivity中的一個按鈕時,用戶可以從MainActivity訪問服務器中的數據,然後具有InsentService類的alarmManager開始從服務器檢索數據以將其顯示在Map活動中。

我想去掉兩個locationManager箱子:

  • 當用戶點擊MainActivity的菜單複選框。
  • 或當他關閉應用程序(不是當用戶更改活動時)。

我如何能區分的onDestroy是否被調用的原因用戶cloeses應用程序或當它被調用的活動之間切換的時候用戶?

我很感激任何幫助。

MainActivity:

public class MainActivity extends ActionBarActivity implements 
     AsyncTaskCallback { 
    TrackingService mService; 
    boolean mBound = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.route_available); 
     // Start the TrackingService class. 
     Intent i = new Intent(this, TrackingService.class); 
     startService(i); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_menu, menu); 
     System.out.println("test onCreateOptionsMenu was invoked."); 

     return true; 
    } 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     MenuItem checkable = menu.findItem(R.id.checkable_menu); 
     checkable.setChecked(isChecked); 
     return true; 
    } 

    // Start and stop the background service. 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.checkable_menu: 
      if (isChecked = !item.isChecked()) { 
       item.setChecked(isChecked); 
       Intent i = new Intent(this, TrackingService.class); 
       startService(i); 
       System.out.println("test if onOptionsItemSelected"); 
      } else { 
       mService.stopTrackingService(); 

      } 
      return true; 

     default: 
      return false; 
     } 
    } 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    Intent i = new Intent(this, TrackingService.class); 
    stopService(i); 

    } 

} 

TrackingService類:

public class TrackingService extends Service implements AsyncTaskCallback, 
     LocationListener { 
    LocationManager lm; 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     detectLocation(); 
     return START_STICKY; 
    } 
    private void detectLocation() { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT) 
       .show(); 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0, 
       this); 
     enableGPS(lm); 

    } 
    @Override 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(wifi_receiver); 
    System.out.println("ABC TrackingService onDestroy() was invoked."); 

    if(lm != null){ 
     lm.removeUpdates(this); 
     System.out.println("ABC TrackingService lm was removed."); 
    }else{ 
     System.out.println("ABC TrackingService lm locationManager is null."); 

    }  
    } 
public void stopTrackingService(){ 
    lm.removeUpdates(this); 
    } 
} 

地圖活動:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{ 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
// To stop the service when the user closed the app in the background and the map ativity was opened.  
     stopAlarm(); 
     Intent i = new Intent(this, TrackingService.class); 
     stopService(i); 
     System.out.println("ABC Map onDestroy() was invoked!"); 

    } 
} 
+0

'isFinishing()'是'Activity'而不是'Context'的一種方法。伊莫,把它稱爲服務沒有任何意義。你想用'isFinishing'來檢查什麼? –

+0

@SaschaKolberg:我想檢查它以防止用戶從MainActivity切換到Map活動時刪除locationManager。 locationManger必須在用戶關閉應用程序時被刪除。有沒有其他方法可以做到這一點? –

+1

爲什麼您在切換活動時會停止服務? –

回答

0

isFinishing()只會告訴你是否沒有完成活動,而不是剛剛暫停。如果您的服務在活動isFinishing()仍然可能會返回false之前被銷燬,儘管應用程序已結束。

您的Service不會因爲您切換到其他活動而被銷燬。它應該獨立於您的活動運行,直到您停止它或系統釋放資源爲止。如果您想防止系統傾銷您的服務,您可以在其onCreate中返回START_STICKY

如果你想殺人,當應用程序被用戶終止服務,可在您的主要活動的onDestroy方法停止它並沒有檢查活動是否已完成:

public class MainActivity extends ActionBarActivity implements 
    AsyncTaskCallback { 

    ... 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     if (isFinishing()) { 
      Intent i = new Intent(this, TrackingService.class); 
      stopService(i); 
     } 
    } 

編輯:

澄清了一些關於你想點什麼之後,這裏是一個可能爲你工作的另一種選擇:

您只需根據m開始的服務ain活動。你永遠不會在你的代碼中的任何地方撥打stopService。但是,您bind服務的這兩個活動和undbind在每個活動的onDestroy方法。請參閱android documentation。當所有綁定已經釋放時,服務應該停止。

+0

好的,我有更多的文字和代碼給probelm。請看看它。 –

+0

@MrAsker更新了我的回答 –

+0

我曾嘗試過。 MainActivity中的onDestroy()僅在關閉應用程序時調用。當我從MainActivity轉到Map Activity時(服務仍然有效,服務onDestroy()未被調用),TrackingService類中的'onDestroy()'被調用。當我返回時(服務仍然有效),但是當我從MainActivity再次返回Map時,由於調用了TrackingService的onDestroy(),服務被銷燬並且locationManager被移除。 –