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.
Related
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.
The problem is I have an article that want to share to other apps, and I want to let the user to choose which app to share to. What I want to share is basically:
the title of the article
the URL of the article
the article content as HTML
the URL with some extra text (such as 'http://foo.com/article share from #FooApp')
All of these fields are optional, but I want to share at least one of them.
Such as when share via SMS or twitter, I want to set the content to part 4. when share via Facebook, I want to set 1, 2, 3 together. And when share via email, I want to set subject as 1 and message as 4.
I know (correct me if I'm wrong) every target intent receiver has it's own logic to pick up the fields it needed. So I want to provide as much information as possible and I wrote the following code:
String message = article.getURL() + " #FooApp";
Intent intent = new Intent().setData(Uri.parse(article.getURL())
.putExtra(Intent.EXTRA_SUBJECT, article.getTitle())
.putExtra(Intent.EXTRA_TEXT, message)
.putExtra(Intent.EXTRA_HTML_TEXT, article.getHTML())
.putExtra("sms_body", message)
...
.setType(HTTP.PLAIN_TEXT_TYPE);
startActivity(Intent.createChooser(intent, "Share to"));
But the problem is, it seems like a trick between setData, putExtra, setType.
For some apps appear in the chooser dialog, when I choose, the confirm share window (of that app) display nothing that I set to the intent. (for some other apps they just say failed to fetch resource)
For the putExtra part, when I add or remove some putExtra code, the target intent receivers diff a lot than I expected.
So the question is: am I doing it the wrong way? Are there some guideline for this problem?
I'm looking for an possibility to call the Instagam application with Intent (like a Camera: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);) and pass to an external application a filter's tag, (as example tag #orchi).
Something similar to iphone hooks: http://instagram.com/developer/iphone-hooks/ or this one http://www.gramfeed.com/instagram/tags#orchi
On the official site is described an API but I need a simple solution to show images (filtered by tag) using an external Instagram application.
I'm trying to launch the Amazon Kindle app from my android application but based on a specific book that the user clicks.
I'm able to determine the books available and which book the user has selected but I have only been able to launch the kindle application (using the package name com.amazon.kindle) to launch the kindle app.
Does anyone know of any additional commands I can send to specify a book to open? I know this is possible as there is a widget on the google play store where the user selects a boo and it creates a button on the homescreen that launches the kindle app and opens the book.
Thanks in advance!
First we need to set the intent to an ACTION_VIEW intent.
Then we need to define an Uri for the data which is actually a link that looks something like: kindle://book/?action=open&book_id=AMZNID0/B000FC1GHO/0/, where in this case the section B000FC1GHO corresponds to the ID of the book.
Finally we can then start the activity. In my case I had to set some flags on the intent to launch a new activity.
The code I'm using is as follows:
if(intent.getAction().contains("BOOK_ACTION_"))
{
Log.w("LOG", "We have a book selected");
bookID = intent.getAction().substring(12);
Log.w("LOG", bookID);
Intent readbook = new Intent(Intent.ACTION_VIEW);
readbook.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri file = Uri.parse("kindle://book/?action=open&book_id=AMZNID0/" + bookID + "/0/");
readbook.setData(file);
context.startActivity(readbook);
}
I'm overriding the onReceive method in this case so that I can perform some additional steps on each book. Presumably because I'm just setting an ACTION_VIEW intent this could have been handles in the other class that does the onClickListener for the imageview that holds the book I want.
I would like to start a default application: browser, contact-book, phone, email, music app, etc. I have found many q/a, like browser opening a specific URL or blank, and here the answer is even "No not possible". But I would like to just open/launch it without telling it to go to a specific URL or sending a mail to someone, etc.
However, I also saw some Home applications where this seems to be working (at least for some apps). On my colleague's device there is for example a different contact-book (no google) which is detected and opened correctly.
I have seen in the Android documentation some intent categories that point to these problems, but these are only >= API.11. So I can't use/test them on my device.
Question: Is it not somehow possible to launch a default application (having the app chooser is of course ok) without providing extra data? If no, what do you think are these Home apps doing (perhaps workarounds are somehow possible).
PS: for the phone app I think, I have a workaround using Intent.ACTION_DIAL without any other information which will open simply the dialer.
UPDATE: I modified the title. Some applications like the address book may not be the same on different devices. So in this case I would like to start the address-book app, whichever this is.
This answer is not a 100% answer, but some workarounds on some typical applications.
Still open are: music player, address book
Browser: I get a list of applications that handle "http"-data intents, and then I look if one is available in the list of preferred applications.
Intent appFilter = new Intent(Intent.ACTION_VIEW);
appFilter.setData(Uri.parse("http://www.google.com"));
List<ResolveInfo> browserInfoList = pm.queryIntentActivities(appFilter, 0);
List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);
if(outActivities.size() > 0) {
for(ComponentName cn : outActivities) {
String cnClass = cn.getClassName();
String cnPkg = cn.getPackageName();
for (ResolveInfo info : browserInfoList) {
if(info.activityInfo.name.equals(cnClass) &&
info.activityInfo.packageName.equals(cnPkg)) {
return cn;
}
}
}
}
In case no default is found, I open a browser chooser dialog, see here.
Phone: as described in the question:
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
You can start apps by the function "startActivity" if you know about the canonical app name
like "android.com.browser". Do this simple by searching for AndroidManifest.xml in the app
source code (look at Codeaurora.com or at github/Cyanogenmod) and grab the app name you want.
After you know about the App name ("Activity") implement the code as follows:
Intent intent = new Intent();
intent.setClassName(this, "com.android.browser");
intent.setCategory(Intent.ACTION_MAIN);
startActivity(intent);
THIS is only a example, sometimes you have to put intent extras or data values, this information can be found in the app's AndroidManifest.xml too.