为什么服务启动时它没有显示messageboxdialog。Android服务类
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlarmService.this.onDestroy();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
}
吐司出现声音正在播放只有showdialog不来,为什么?
您无法从服务创建对话框。参考这篇文章:http://stackoverflow.com/questions/5126868/showing-an-ok-dialog-box-from-a-service-receiver-in-android – AedonEtLIRA 2012-02-17 18:13:27
我想要的是让我看到一个按钮,当服务正在运行停止它,所以你认为从服务我必须创建意图,并调用另一个活动,该活动调用onDestroy()方法将停止服务? – Samuel 2012-02-17 18:22:50
你根本不需要这项活动。该服务有一个方法,** stopService(Intent)**。只是打电话。 http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29 – AedonEtLIRA 2012-02-17 18:26:01