我想從我的MainActivity
到TrackingService
類通過上下文來檢查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!");
}
}
'isFinishing()'是'Activity'而不是'Context'的一種方法。伊莫,把它稱爲服務沒有任何意義。你想用'isFinishing'來檢查什麼? –
@SaschaKolberg:我想檢查它以防止用戶從MainActivity切換到Map活動時刪除locationManager。 locationManger必須在用戶關閉應用程序時被刪除。有沒有其他方法可以做到這一點? –
爲什麼您在切換活動時會停止服務? –