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

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.

Related

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

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)

How to share different texts based on package name in Android 10?

My app shares a specific URL to open in other apps, but I want to use a custom URL depending on what app the user is sharing it with. For example, with Gmail I want to use myurl.com?src=gmail, and with FB I want to use myurl.com?src=fb etc.
This normally would have worked with this famous method: https://stackoverflow.com/a/18068122/3015986.
However, in Android 10, that solution no longer works anymore: https://medium.com/#AndroidDeveloperLB/this-wont-work-anymore-on-android-q-1702c19eb7bb
So, what other options are there?
Well there are always options. In this particular case(with the given conditions regarding URL query parameter) I would suggest to create separate sharing method for each platform you want to share your content with rather than use the Android default sharing method.
For example here is a custom sharing for Facebook, etc. The method is quite time consuming though.
But there is a better option to handle such cases. It is a relatively new method to get info about which app was chosen. It is easy. Basically you will need to:
Create a PendingIntent for a BroadcastReceiver and supply its IntentSender in Intent.createChooser() like this:
Intent share = new Intent(ACTION_SEND);
...
PendingIntent pi = PendingIntent.getBroadcast(myContext, requestCode,
new Intent(myContext, MyBroadcastReceiver.class),
FLAG_UPDATE_CURRENT);
share = Intent.createChooser(share, null, pi.getIntentSender());
Receive the callback in MyBroadcastReceiver and look in Intent.EXTRA_CHOSEN_COMPONENT like this:
#Override public void onReceive(Context context, Intent intent) {
...
ComponentName clickedComponent = intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT);
}
More info here
Though you cannot change your URL for each specific case because the sharing event has already occurred, you can implement separate API call or URL link using which you can gather analytics data, change the outcome of the URL click you sent(if you control the backend, of course) or just show a customized message to the user of your app.
Keep in mind, that for some usecases of this method, URL you share should be unique per user(for ex. to change the outcome of the URL click etc).
Hope it helps.

How to send data via WhatsApp using android app?

Upon clicking some view in my app, How to send data via Whatsapp.
If WhatsApp is installed, it should be opened or else Google Play Store should be opened to facilitate WhatsApp installation.
I guess this is what you're looking for.
Allowing Other Apps to Start Your Activity
Check this link for info.
The implementation of this is very easy, you can even get Intent type in your activity, using this:
Intent mIntent = getIntent();
Uri data = mIntent.getData();
// Figure out what to do based on the intent type
if (mIntent.getType().indexOf("image/") != -1) {
// Handle intents with image data ...
} else if (mIntent.getType().equals("text/plain")) {
// Handle intents with text ...
}
Also, do some research on Google before posting a question, these things are answered before.

How to launch Amazon Shopping app in Android app?

I'd like to be able to launch the Amazon Shopping app from my android application. How is this possible? What parameters would need to go into the Intent? Here is a link to the Amazon Shopping app: https://play.google.com/store/apps/details?id=com.amazon.mShop.android.shopping&hl=en
In addition, how would it be possible to pass a deep-link parameter so that it lands on a specific product page? Thank you!
I'd like to be able to launch the Amazon Shopping app from my android
application. How is this possible? What parameters would need to go
into the Intent?
You can use PackageManager#getLaunchIntentForPackage
startActivity(getPackageManager().getLaunchIntentForPackage("com.amazon.mShop.android.shopping"));
In addition, how would it be possible to pass a deep-link parameter so
that it lands on a specific product page?
It depends on whether Amazon app implements deep link and exposes intent-filter to external app. I assume it's not possible, but maybe you can ask Amazon.
The problem with using
startActivity(getPackageManager().getLaunchIntentForPackage("com.amazon.mShop.android.shopping"));
is that it assumes that the user has the android app installed. If it's not there it will fail. So, I decided to use a uri. My first attempt was to use the amazon documentation Link to Amazon from within Your App
but that didn't work so well. It looks like it only searches for apps, not all products. When I tried to use the asin parameter for a non-app product it did not work. So I did the following and it gave me what I wanted.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.amazon.com/Red-blue-Anaglyph-3D-Glasses-game-Extra/dp/B003LWYGPE/ref=pd_sim_23_1?_encoding=UTF8&pd_rd_i=B003LWYGPE&pd_rd_r=3REW4891981B4R6WAB66&pd_rd_w=NcbkD&pd_rd_wg=GDhOT&psc=1&refRID=3REW4891981B4R6WAB66"));
startActivity(browserIntent);
It opened the search in a browser with an option to open the app. I suppose one could attempt to go the amazon app route first and, if it fails, open this browser version.
if you want to deeplink to the detail page ,first you should find the product_id for amazon. and try this scheme:
com.amazon.mobile.shopping://content/item?id=<some valid id>
All code trying here is:
jumpUrl?.let { it ->
try {
val intent = Intent(Intent.ACTION_VIEW)
val findStr = "/dp/"
val findIndex = it.indexOf(findStr)
if (findIndex != -1) {
intent.data = Uri.parse(
"com.amazon.mShop.android.shopping://www.amazon.com/products/${
it.substring(
findIndex + findStr.length
)
}"
)
} else {
intent.data = Uri.parse(it)
}
intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK
intent.let { tent ->
context.startActivity(
tent
)
}
} catch (exception: ActivityNotFoundException) {
exception.printStackTrace()
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(it)
intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
}
My references is : iPhone iOS Amazon Scheme / URI Not Working Anymore amzn:// version 4.3.1 Deep Linking

Launching Android Netflix App And Passing Video Id

In the app I am working on I want to support Netfilx streaming. I intend on doing this by simply starting Netflix and passing a specific URI so it plays a specific video when started. Simple right? Well, the issue is I'm not sure how to pass the video id info in the Intent I use to start the Activity.
I've read the post here , but am unsure where to use this. I used Intent.setData() since it accepts a URI, but to no avail.
Here is what I have been doing (I am hard coding the movie data, this is just for testing purposes) :
// the Netflix intent
Intent intent = getPackageManager().getLaunchIntentForPackage("com.netflix.mediaclient");
//the uri
Uri uri = Uri.parse("http://movies.netflix.com/WiPlayer?movieid=70266228&trkid=13462049&ctx=0%2C1%2Ce2bd7b74-6743-4d5e-864f-1cc2568ba0da-61921755");
intent.setData(uri);
//launches but does not go to the video
startActivity(intent);
I've also tried using the URI protocol in the link above like so:
Uri uri = Uri.parse("nflx://movies.netflix.com/WiPlayer?movieid=70266228&trkid=13462049&ctx=0%2C1%2Ce2bd7b74-6743-4d5e-864f-1cc2568ba0da-61921755");
but still am not seeing the video play.
I feel like I am missing something simple here, although I have had very little luck Googling for this, I could find next to nothing about starting the Netflix Android app from another application. The Netflix developer resources don't have any info on this.
Does anyone have any suggestions on how I can do this or where I should be looking for documentation on this? Any help would be appreciated. Thanks much!
Just some Android Apps intent names to help anyone.
Format: appName, packageName, className
Skype: com.skype.raider, com.skype.raider.Main
Netflix: com.netflix.mediaclient, com.netflix.mediaclient.ui.launch.UIWebViewActivity
ESexplorer: com.estrongs.android.pop, com.estrongs.android.pop.view.FileExplorerActivity
Youtube: com.google.android.youtube,com.google.android.youtube.HomeActivity
Chrome: com.android.chrome,com.google.android.apps.chrome.Main
VLC: org.videolan.vlc, org.videolan.vlc.gui.MainActivity
MBOXSettings: com.mbx.settingsmbox, com.mbx.settingsmbox.SettingsMboxActivity
I've managed to do this. With the following code. First you need the movieId (or videoId) for netflix, then compose the URL to watch.
String netFlixId = "43598743"; // <== isn't a real movie id
String watchUrl = "http://www.netflix.com/watch/"+netFlixId;
Then with this intent
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.netflix.mediaclient", "com.netflix.mediaclient.ui.launch.UIWebViewActivity");
intent.setData(Uri.parse(watchUrl));
startActivity(intent);
}
catch(Exception e)
{
// netflix app isn't installed, send to website.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(item.url));
startActivity(intent);
}
I haven't tried with TV Shows yet. But this works beautifully. If you want to send to the profile page of the movie.. Send it to a url formatted this way
http://www.netflix.com/title/324982
I also found out how to search for a title.
try {
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setClassName("com.netflix.mediaclient", "com.netflix.mediaclient.ui.search.SearchActivity");
intent.putExtra("query", item.label);
startActivity(intent);
}
catch(Exception e)
{
Toast.makeText(this, "Please install the NetFlix App!", Toast.LENGTH_SHORT).show();
}
Here is how to start a movie from an ADB command for com.netflix.mediaclient
adb shell am start -n com.netflix.mediaclient/.ui.launch.UIWebViewActivity -a android.intent.action.VIEW -d http://www.netflix.com/watch/60000724
I still can't find a way to do it for com.netflix.ninja (Netflix for Android TV)

Categories

Resources