Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open browser at specified URL using ACTION_VIEW not working when chrome is default browser

I have 2 browsers installed on my Android device 1) default and 2) Chrome

When I run the following code I get a 'Complete action using' selector...

String url = "http://www.google.com/search?q=" + query; 
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

The default Android browser works, in that a browser opens at the specified URL. However, when I open using Chrome browser, Chrome simply opens... it seems to ignore the specified URL.

Is this a problem with Chrome, or my code?

Cheers

like image 619
Michael Avatar asked Nov 03 '22 01:11

Michael


1 Answers

The problem was my query variable. It appears Chrome did not accept the format of the query string, where the default Android browser did.

The get the code to work I had to URL encode the query...

query = URLEncoder.encode(query, "UTF-8");
like image 66
Michael Avatar answered Nov 08 '22 06:11

Michael