当用户点击警报按钮打开手机浏览器并在Android应用程序上显示URL

时间:2013-12-19 11:44:37

标签: java android

当用户点击提醒按摩知道更多按钮打开手机浏览器并在Android应用程序上显示我的网站的URL我所做的。我需要代码。我尝试了许多代码,比如Web视图和其他代码,但没有工作。请帮帮我!

以下代码无效。

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_about:


            AlertDialog.Builder dialog = new AlertDialog.Builder(NoteEdit.this);
            dialog.setTitle("About");
            dialog.setMessage("Hello!);
            dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    }});
            dialog.setPositiveButton("KNOW MORE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String url = "http://www.google.com";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                 }
                });

3 个答案:

答案 0 :(得分:2)

以下代码完美适用于我的情况: 创建 定义private static final int DIALOG_EXIT = 1; 现在创建

protected Dialog onCreateDialog(int id) {

    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(this);

    switch (id) {
    case DIALOG_EXIT:
        return new AlertDialog.Builder(MainActivity.this)
                .setTitle("About")
                .setMessage("Hello")
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("Know More",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                /*Uri uri = Uri
                                        .parse("http://www.google.com");
                                Intent intent = new Intent(
                                        Intent.ACTION_VIEW, uri);
                                startActivity(intent);*/
                                 String url = "http://www.google.com";
                                    Intent i = new Intent(Intent.ACTION_VIEW);
                                    i.setData(Uri.parse(url));
                                    startActivity(i);
                            }
                        }).create();
    }
    dialog = builder.show();
    return dialog;
}

你只是从菜单项选择功能调用了这个;

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
    case R.id.setting:
        showDialog(DIALOG_EXIT);
        break;
    }
    return true;
}

试试这可能会对你有帮助。

答案 1 :(得分:1)

此代码适用于我:

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(i);

答案 2 :(得分:1)

您需要显示对话框。最后,dd行

dialog.show()

另外,在

dialog.setMessage("Hello!);

你错过了一个结尾的引用。