Chose browser open url in android studio - android

I have a link url https://www.google.com.vn/
I want when click link url I can check if my device installed chrome it will always open url with chomre else it will open url with default browser ( not show dialog to chose chrome or internet brower...)
How I can do it?

Try the following code to get the list of activities which can handle your intent and then use Intent.setComponent() to explicitly select the application that you want to launch
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);

Related

How to create a link to already installed user applications

In my widget, when the widget is opened, I want it to open up a new page that shows links to the users already installed applications. The users must be able to add and delete the links as needed and the links would be updated if that application was deleted from the users device. I have already created the ability in my widget to open a new class but I don't know on how to go about linking to user applications. How can I do this?
You have to get the Activity of the app and link it with URI.
For example:
Uri.parse("market://details?id=" + facebookpackagename);
will load the Facebook app from within your app.
Try this code:
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
String currAppName = pm.getApplicationLabel(packageInfo).toString();
//This app is a non-system app
}
else{
//System App
}
}

Show browser list when opening a link in android

I want to open some links in an external browser. Before opening, I want to display the list of browsers on the device. I did this functionality using Intent.ACTION_VIEW. But as per my app's requirement, I want to display the list of browsers even if there is only one browser application on the device. Does anyone have any idea about this? Thanks.
If you have one browser application , the chooser will not launched , the URL will be loaded on that browser application. If you have two or more browser applications, the android system launch an IntentChooser the show the list of all browser apps installed on the device , so the user can choose his preferred browser to load the URL :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent);
EDIT :
to create your custom Intent Chooser :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
// Always use string resources for UI text. This says something like "Share this photo with"
String title = getString(R.string.chooser_title);
// Create and start the chooser
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);
refer this
Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.maidofknowledge.com"));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
settings>apps>clear default (
this is if you have previously set "use as default app" for chrome
these 2 images are my source:
Message to select browser or select default:
http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-03.jpg
Clear default browser, show intent chooser again:
http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-02.jpg

Override default application to handle intent

I have a code for opening URL with browser:
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
My question is, if user has set up default app for opening url, is there a way to override the default app and show application choose dialog instead?
i just found a solution. it was a so simple:
startActivity(Intent.createChooser(intent, title));
I do not know the exact answer to your question, but there is a bit more complicated decision. You can get a list of browsers, that installed on smartphone:
PackageManager manager = getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(myIntent,PackageManager.MATCH_DEFAULT_ONLY);
And then create a choose dialog manually.
P.S. Maybe there is a more simple solution ...

Checking for PDF intent on Kindle Fire returns nothing?

I'm opening a PDF file through an intent; in order to check beforehand if a capable application is installed, I use this code:
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
This works fine on my Galaxy Nexus, however, the list is empty when run on a Kindle Fire. But if I skip the check, the PDF is opened just fine! How so?
Is there any other way to check for PDF viewing capablities? (The PDF is included in the APK asset's folder, and I would like to only copy it into the data folder if I know it can be displayed...)
Or should I just stop bothering to check as most Android devices can view PDFs anyway?
I'm fairly sure you can't query for that intent without setting the actual data the Intent should be able to operate on.
This link also seems to deal with the same problem.

Android - Call default browser with and redirect to a designated url

Hi
I want to write an app to call default browser and redirect to a designated url.
Any suggestion to 1)call the default browser, 2)redirect to a designated url.
Thanks
you just want to launch an ACTION_VIEW intent with the Uri of the webpage as your data element :
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("http://www.bbc.co.uk"));
startActivity(httpIntent);
To open the default browser use an Intent with the action VIEW. To tell the browser which page to load us the data-part of the Intent.
Example:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(browse);
Since this is a basic task in Android you might want to read some basics about Intents in Android.

Categories

Resources