Can't open link like "intent://" in android app - android

I want to open link like "intent://qr.nspk.ru/url-something/#Intent;scheme=someScheme;package=com.example.android;end#Intent;scheme=someScheme;end" from an android application.
If I write something like this:
val i = Intent(Intent.ACTION_VIEW, Uri.parse(androidUrl))
startActivity(i)
I get this error:
No Activity found to handle Intent { act=android.intent.action.VIEW
dat=intent://qr.nspk.ru/url-something/#Intent;scheme=someScheme;package=com.example.android;end#Intent;scheme=someScheme;end
But If I go to this link from browser, it opens and it open an app that I need. Can you help me to understand why I can't open link from app and how to fix it?

The intent: scheme is used for the browser to encode an Android Intent. If you want to use this URL from an Android app, you need to create an Intent from the URL like this:
val i = Intent.parseUri(androidUrl, Intent.URI_INTENT_SCHEME)
startActivity(i)

Related

Android 12 open link in Chrome intent

Since Android 12, links can only be opened with one "approved" app.
As of right now I have my app's supported urls defined in AndroidManifest.xml as intent-filter.
Also I have a button in my app that creates an intent to open a link in browser.
With Android 12 limitations though, if I have the url tied to my app, clicking the open in browser button does re-open my app, which is quite an unwanted behavior.
Is there a way to force a url open in chrome (or other browser)?
I checked the android developer documentation but have not found anything about it.
Thank you
You can specify the default browser explicitly as the package to handle your intent:
val defaultBrowserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
// This would be Chrome if it's the selected default browser
val defaultBrowserPackageName = packageManager.resolveActivity(
defaultBrowserIntent,
PackageManager.MATCH_DEFAULT_ONLY
)
?.activityInfo
?.packageName
val yourIntent = Intent(context, ...).apply {
package = defaultBrowserPackageName
}

How can I pass a particular link when I use PackageManager?

I want to pass a link of a video and want YouTube app to play that video directly and I want to use following code:
btn3.setOnClickListener {
val i = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube")!!
startActivity(i)
}
I don't want to use following code:
startActivity(Intent(Intent.ACTION_VIEW,Uri.parse("link of video")))
Instead of querying the launch Intent, which does not take any arguments, you can call setPackage() on your ACTION_VIEW Intent to tell the OS to open the desired app:
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("link of video")).apply {
setPackage("com.google.android.youtube")
}
You should be careful though, in case the YouTube app is not installed on the device your code will throw an ActivityNotFoundException. The best way to guard against this is using PackageManager.queryIntentActivities() which will return an empty list if there are no Activities that can handle the Intent.

how to use the intent anchor to launch my app with extra data(parameters) in android

I have ever tried to launch my app using URI schema in javascript like this:
window.location = "myapp://api/image?imageurl=hogehoge"
but it failed in android.I don't know why. it's ok in iphone...
so I changed to use the intent anchor to launch my app in android as described here. I'm able to get it to launch my app using this syntax,
<a href="intent://#Intent;scheme=http;package=com.example.myapp;end">Launch my app</>
but if I use the intent anchor , how can I pass the extra data ("api/image?imageurl=hogehoge") ?
maybe i can add "S.imageurl=hogehoge" to the href but how can i pass this →"api/image?"
Try something like this:
Launch my app

How to use startActivity to open data uri

I want to open a data: url containing a pdf in an activity in my android application. I have something like the following code:
String url = "data:application/pdf;base64,JVBERi0xLjIgDQol4uPP0w0KIA..."; // shortened for brevity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
I am seeing an error:
error opening uri: No Activity found to handle Intent {
act=android.intent.action.VIEW dat=data:application/pdf... (followed
by the rest of the data url).
How can I resolve this?
Since approximately zero apps in existence will support that scheme, you will need to decode the PDF yourself, write it to a file, and then open the PDF viewer on the file.
You need to have an app that can handle opening pdf documents. If you still wish to do this catch an ActivityNotFoundException and tell the user to download one.

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