How to send data via WhatsApp using android app? - android

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.

Related

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 check sent text in OnActivityResult?

I need to be able to check if the user has sent the text I set in my code;
Intent messageIntent = new Intent(Intent.ACTION_SEND)
messageIntent.putExtra(Intent.EXTRA_TEXT, "Text I want to send");
or has changed the prefixed text (on the SMS client app, email client app, or the app that gets launched by the intent) before sending it.
I need to know this because sharing my game link will give a reward to the user, so I need to check if that link is correctly shared.
I would appreciate any answer that could help.
Thanks.
ACTION_SEND isn't documented to return a result. So starting the intent with startActivityForResult() will return the default result: RESULT_CANCELED. That is, unless the receiving app has implemented support for returning a result; which is highly unlikely because it's not part of the documented behavior.
In summary, what you want to do is not possible with any of the common Intents.
You can try this...
String message = "Your Message";
intent.putExtra("IDENTIFIER", message);
And where you are going to receive the message...
String messageReceived = getIntent().getExtras().get("IDENTIFIER").toString();

Intent.ACTION_VIEW - Multiple files

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);

How to open kindle eBook from Android?

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.

Android: starting a default application or one which may be different on different devices

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.

Categories

Resources