【发布时间】:2015-08-30 08:41:57
【问题描述】:
我有一个 trackingService locationManager 组件,它在后台处理,它是从 MainActivity 活动启动的。同时,我在 MainActivity 中有另一个组件(访问组件)来从服务器检索这些数据并将其显示在 Map 活动中。用户可以从 MainActivity 访问服务器中的这些数据,当他单击其中的按钮时,带有 InsentService 类的警报管理器开始从服务器检索数据以每 15 秒将其显示在地图活动中(警报管理器正在停止当我在后台打开地图活动杀死应用程序或离开地图活动时)
我试图在两种情况下删除locationManager:
- 当用户点击 MainActivity 菜单中的复选框时。
- 或当他关闭应用程序时(不是当用户更改活动时)。
我在调用onDestroy() 时遇到问题,因为当我在活动之间切换两次时服务没有停止(这是一种奇怪的行为。)
- 当我在启动应用程序后直接使用 MainActivity 终止应用程序时,它会停止(onDestroy() - MainActivity 被调用)。
当我启动应用程序并进入 MainActivity->Map 活动并在后台使用打开的 Map 杀死应用程序时它停止(onDestroy() - 调用 Map)。
当我进入 MainActivity->Map 活动 -> MainActivity 时它停止了,我在后台使用打开的 MainActivity 杀死了应用程序(但在这种情况下,没有调用 MainActivity 的 onDestroy。)。
当我转到 Main->Map->Main->Map 时它并没有停止,我在后台使用打开的 Map 活动杀死了应用程序,因为在这种情况下调用了地图活动的 onDestroy()。
有人可以解释一下onDestroy() 的这种奇怪行为吗?
感谢您的帮助。
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);
}
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!");
}
}
【问题讨论】:
-
在“开发者选项”中打开“不保留活动”
-
@pskink:那是什么意思?我该怎么做?
-
向google叔叔问:android开发者选项
-
我正在我的设备中运行代码,并且启用了 Devloper 选项。你的意思是像这样的greenbot.com/article/2457986/…。但这与问题有什么关系?我必须禁用此选项吗?
-
你必须启用“不要保留活动”,而不是禁用
标签: android