小编Ig0*_*r82的帖子

从android中的服务弹出警报对话框

我正在尝试构建活动感知应用程序.我使用Android开发人员的活动识别示例作为起点.在此示例中,用户开始移动通知的那一刻出现,并请求打开GPS.我正在尝试用警报对话框和一些声音替换此通知.我找到的简单且最常见的警报框对我不起作用,因为从服务中创建一个不合格是不可能的.感谢Harsh给我一个活动的想法.我将在它工作的那一刻发布代码.提前感谢您承诺从服务调用的警报框的工作代码:1)服务文件中的代码:

    Intent intent;
    intent = new Intent(this, MyAlert.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

2)警报活动.java文件中的代码:

    public class MyAlert extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_alert);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set title
    alertDialogBuilder.setTitle("Your Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Your Message")
            .setCancelable(false)
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    stopService(getIntent());
                    dialog.cancel();
                    finish();
                }
            });

    // create alert dialog
    AlertDialog alertDialog …
Run Code Online (Sandbox Code Playgroud)

service android android-alertdialog

4
推荐指数
1
解决办法
8381
查看次数

标签 统计

android ×1

android-alertdialog ×1

service ×1