Zan*_*hna 1 android android-preferences android-service android-activity
我在我的偏好活动中给出退出选项.在该选项中,我想取消绑定并停止服务.但我的申请被关闭但服务不会停止.我从另一个活动开始并绑定服务.在偏好活动中,我没有开始或约束.
这是我的偏好活动代码:
Preference exit = findPreference("Exit");
exit.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
new AlertDialog.Builder(SettingsActivity.this)
.setTitle("Exit :")
.setMessage("Are You Sure??")
.setNegativeButton("No", null)
.setPositiveButton("Yes",
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
stopService(new Intent(getApplicationContext(), MyService.class));
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.show();
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
但我的服务并没有停止.我该如何停止服务?请求帮助我...
编辑:
得到了错误
01-24 10:20:30.699: E/AndroidRuntime(18503): FATAL EXCEPTION: main
01-24 10:20:30.699: E/AndroidRuntime(18503): java.lang.IllegalArgumentException: Service not registered: com.androidhive.musicplayer.SettingsActivity$1@405c7af8
01-24 10:20:30.699: E/AndroidRuntime(18503): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:891)
01-24 10:20:30.699: E/AndroidRuntime(18503): at android.app.ContextImpl.unbindService(ContextImpl.java:901)
01-24 10:20:30.699: E/AndroidRuntime(18503): at android.content.ContextWrapper.unbindService(ContextWrapper.java:352)
Run Code Online (Sandbox Code Playgroud)
我改变的代码:
getApplicationContext().stopService(new Intent(SettingsActivity.this, MyService.class)); getApplicationContext().unbindService(serviceConnection);
Run Code Online (Sandbox Code Playgroud)
通常,服务会在您上次活动中停止onPause或停止.onDestroy
我在我的一个项目中使用此代码:
@Override
protected void onDestroy() {
super.onDestroy();
getApplicationContext().unbindService(service);
}
Run Code Online (Sandbox Code Playgroud)
退出我的应用时,此代码退出我的服务.
| 归档时间: |
|
| 查看次数: |
3425 次 |
| 最近记录: |