从活动启动浏览器,然后退出应用程序,打开浏览器

时间:2014-07-29 14:17:37

标签: java android

第一!
好的,我正在开发一个应用程序“更新系统”,用于未在商店托管的应用程序。我想要做的是从活动启动浏览器然后退出应用程序,打开浏览器。这可以做到,如果是这样,你能指出我正确的方向吗?
编辑:
我不知道这是否会改变我希望通过AlertDialog这样做的任何内容。

2 个答案:

答案 0 :(得分:4)

这是您启动浏览器的方式:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somewebsite.com"));
startActivity(browserIntent);

这就是你完成活动的方式:

finish();

完成一项活动与退出整个应用程序不同,因为堆栈中可能有许多活动。但是,您可以(并且应该)将此任务留给系统 - 当没有足够的可用资源时,您的应用程序进程将自动终止。 供参考:Quitting an application - is that frowned upon?

编辑:使用AlertDialog的示例:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Launch a website?");
builder.setPositiveButton(getString(R.string.yes),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("http://www.somewebsite.com"));
            startActivity(browserIntent);
            finish();
        }
    }
);
builder.setNegativeButton(getString(R.string.no),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //some other thing to do
        }
    }
);

AlertDialog dialog = builder.create();
dialog.show();

答案 1 :(得分:1)

您需要创建一个新意图,在设备上打开浏览器,并在启动新意图后完成当前活动。

Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse("http://www.google.com"));
startActivity(browserIntent);
finish();