我想从我的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必须在用户关闭应用程序时被删除。有没有其他方法可以做到这一点? –
为什么您在切换活动时会停止服务? –