サービスがアクティブになっているときにメッセージボックスダイアログボックスが表示されないのはなぜですか?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();
}
});
}
サウンドが再生されているように見えるのは、ショーダイアグラムが来ていないのはなぜですか?
サービスからダイアログを作成することはできません。この投稿を参照してください:http://stackoverflow.com/questions/5126868/showing-an-ok-dialog-box-from-a-service-receiver-in-android – AedonEtLIRA
私が欲しいのは私にボタンを表示することです。サービスが停止しているので、サービスからインテントを作成し、そのアクティビティでonDestroy()メソッドがサービスを停止する別のアクティビティを呼び出す必要があると思いますか? – Samuel
アクティビティはまったく必要ありません。このサービスにはメソッド** stopService(Intent)**があります。ちょうどそれを呼び出します。 http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29 – AedonEtLIRA