可以显示对话框,在服务[Android应用程序] [英] Is possible to Show Dialog box in Service [android app]?
问题描述
我是一个新到Android,所以我试图发展,显示后特定时间的用户通知的应用程序,我不知道:是否可以使用内部服务对话框!还是不行!...
I'm a new to android, so I'm trying to develop an application that shows a notification to the user after specific time, I don't know: Is it possible to use a dialog box inside Service ! or not !...
我的目标是:要问他一个问题后,具体的时间,在我的code图所示,
My Goal is: to ask him a question after specific time, as shown below in my code,
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyAlarmService extends Service {
@Override
public void onCreate() {
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
.show();
}
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
.show();
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
// For Notification
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Conformation");
alertDialog
.setMessage("Are you sure you want to Expand Report Region?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Sending a Message to server that the plaintiff wants to
// expand report region
}
});
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
}
}
此外,我不知道我将使用服务或广播接收器?
Also I don't know shall I use Service or BroadcastReceiver ??
任何建议,在此先感谢=)
any suggestion, thanks in advance =)
推荐答案
广播接收器 - >服务 - >活动这个顺序是好的。
显示警告对话框,而不pressing任意按钮调用方法的ShowDialog()在活动的onCreate()方法。
BroadCast Receiver -- > Service --> Activity this sequence is good. For displaying alert dialog without pressing any button call method, showDialog() in Activity's onCreate() method.
这篇关于可以显示对话框,在服务[Android应用程序]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!