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.
Related
I'm going to develop a simple app to list .jpg files and after a click call an Intent.ACTION_VIEW, below the code:
File imageFile = new File(filename);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(imageFile), "image/jpeg");
startActivity(i);
The intent works properly, in fact in app I receive the message to select an app to show the selected image. But I noticed that after the choice, for example with Google Photos, not all function are enabled. For example the share function is not visible, has someone already noticed this behavior?
Some other details; if I choose Google Photos I only have the "Info" and "Guide and Feedback" options. If I choose gallery app (I have a Samsung Ace 3) I have only "Info" and "Set as wallpaper" options.
Note: if I open the same image directly from Google Photos all functions are enabled...
The ACTION_VIEW intent calls another application to view the content that you present, in your case a JPEG image. If there is more than on application that can handle that Intent, Android lets the user choose an app to display the image. Once the application receives the Intent, it is up to that application to display the content how it sees fit.
Basically, you have no control over what functions are available with Intent.ACTION_VIEW. If you want more control, you can create your own Activity in which you can view the image and provide whatever functions you would like.
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 have a requirement to show multiple files (2 or more each of images, music or video, but only one type). Given a series of thumbnails or filenames, the user should be able to select a chekbox and preview the selection. IF the user selects multiple images, I want to be able to show ONLY those images selected. If he selects multiple mp3 files, I want to be able to play ONLY those songs.
If the user selects only one file, that's easy to do:
Intent i = new Intent(Intent.ACTION_VIEW);
if (someType == IMAGE) {
i.setDataAndType(Uri.fromFile(imageFile),"image/*");
}
else
if (sometype == VIDEO) {
i.setDataAndType(Uri.fromFile(videoFile),"video/mp4");
}
else if (someType == MUSIC) {
i.setDataAndType(Uri.fromFile(musicFile),"audio/mpeg");
}
startActivity(i);
However, if the user selects two or more of one type, how can I architect the intent to use whatever image/audio/video apps the user may have installed? Don't want to write custom players/viewers as I know an existing app can do this.
I am trying to mimic the functionality of a cloud app called "AllShare Play" (https://www.samsung.com/us/2012-allshare-play/) . This app does exactly what I need to do in my app - allows the user to "preview" multiple files and uses just the standard "Gallery" app for images, standard music app for songs, etc. In each case, only the files selected are shown or played using standard apps each Android device is loaded with, so I know it's possible to do without writing a viewer or player.
Anyone have any ideas how to send multiple files via Intent.ACTION_VIEW to an external app?
There is no android standard for send multible to ACTION_VIEW.
But there is a standard for ACTION_SEND_MULTIPLE where the files are transfered via EXTRA_STREAM.
for a working sendmultible example see Secure-Photo-Viewer: you select multible images from a gallery app and send them to Secure-Photo-Viewer.
If your app is the only sender and receiver of this intent you can use the same mechanism with ACTION_VIEW, too.
To learn more on intent communication you can use the intent-intercept app that allows sending and receiving many types of intents
here is the code i use to send multible images
Intent sendIntent = new Intent();
sendIntent.setType("image/*");
if (selectionCount == 1) {
Long imageId = mSelectedItems.first();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(EXTRA_STREAM, getUri(imageId));
} else {
sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
ArrayList<Uri> uris = new ArrayList<Uri>();
for (Long mSelectedItem : mSelectedItems) {
uris.add(getUri(mSelectedItem));
}
sendIntent.putParcelableArrayListExtra(EXTRA_STREAM, uris);
}
startActivity(sendIntent);
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.
in my android application i want to add the functionality the user to buy song from amazon. The easiest way to do is i think to use amazon mp3 application to communicate with amazon store. I found this piece of code from default music player
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(MediaStore.INTENT_ACTION_MEDIA_SEARCH);
i.putExtra(SearchManager.QUERY, mSong.getArtits() + " " + mSong.getName());
i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, "artist");
i.putExtra(MediaStore.EXTRA_MEDIA_ALBUM, "album");
i.putExtra(MediaStore.EXTRA_MEDIA_TITLE, mSong.getName());
i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "audio/*");
startActivity(Intent.createChooser(i, "Search for " + mSong.getName()));
which shows menu to select where to search for your song (browser, youtube, amazon mp3). But here are some things i want to do -
I don't want to show the whole pop up screen but only amazon search, what intent i should use to search directly in amazon mp3.
How can i send my affiliate partner key to amazon mp3 so it can included it when querying amazon.
Shazam is using directly amazon mp3 but i couldn't find any information what intent i should use. Thanks in advance any help will be very helpful.
It's not a public API; Shazam apparently have a private arrangement with Amazon. It might be worth contacting Amazon directly, since they have a lot of public APIs, and this one would certainly seem to be in their interests. But like any large company, I wouldn't hold your breath for a response.
as for 1., you can try to determine the correct activity by evaulating the list of matching activities like this (i being the intent you created):
List<ResolveInfo> info = getPackageManager().queryIntentActivities(i, 0);
String packageName=null, className=null;
for ( ResolveInfo r: info){
if ( r.activityInfo.packageName.startsWith("com.amazon.mp3")){
packageName=r.activityInfo.packageName;
className=r.activityInfo.name;
break;
}
}
if ( packageName != null && className != null)
i.setClassName(packageName, className);
startActivity(i);
This is sort of a hack since one should not rely on the package name starting with a certain fixed string, but in fact it will probably work for long. Just be prepared in your code to deal with it changing (android will automatically display the activity chooser if the Amazon activity is not identified).
as for 2., sorry, I have no information about this.