I have a problem with sharing on Android M and exactly with creating intent chooser with filter. I've create a standard text share intent:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, getSharingText());
return intent;
Then I've applied the filter for chooser:
List<Intent> targetedShareIntents = new ArrayList<>();
List<ResolveInfo> resolves = getActivity().getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolves) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(mPromoIntent);
if (!packageName.equals("com.facebook.katana")
&& !packageName.equals("com.vkontakte.android")) {
ComponentName componentName = new ComponentName(packageName, resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent.setComponent(componentName));
}
}
if (targetedShareIntents.isEmpty()) {
return null;
}
Intent chooser = targetedShareIntents.remove(0);
return Intent.createChooser(chooser, chooserText)
.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
And the I've started the standard chooser ChooserActivity using chooser intent
startActivity(mAppsChooserIntent);
But on Android M it doesn't show chooser, it takes the first intent (in my case bluetooth) and shares with it.
I've looked through the ChooserActivity class, which in Android MNC is much bigger than in Android L and didn't find a solution where.
Does somebody know the answer or it's Android M preview bug?
Related
I am working with this code which Ragu Swaminathan helped me with on my original post found at: How to show both texting and dialer apps with a single Intent on Android?.
final List<Intent> finalIntents = new ArrayList<Intent>();
final Intent textIntent = new Intent(Intent.ACTION_VIEW);
textIntent.setType("text/plain");
textIntent.setData(Uri.parse("sms:"));
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(textIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(textIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
finalIntents.add(intent);
}
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:121"));
Intent chooserIntent = Intent.createChooser(
callIntent, "Select app to share");
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, finalIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I have this code that brings up snackup separating sms and dialer apps installed on users phone and allows them to pick one to either send a message / call a person.
What I want to know is, is it possbile to add another section on this snackbar that checks if the user has whatsapp installed or another specfic app installed.
Also, being new to android, I have tried playing around with the code but ended up messing it up. another thing that I would like to do is create sections like theses;
Sms apps:
.....
.....
.....
Dialer apps:
.....
.....
.....
Whatsapp:
.....
Any help is welcomed, please let me know if my question is not clear.
Edited;
You can use getPackageInfo method
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageInfo(certainAppPackageName, 0);
if (pi != null) {
//app is installed, do smth
}
Google play links exist package names.
For example:
https://play.google.com/store/apps/details?id=org.telegram.messenger
where org.telegram.messenger is a package name
How can i remove Google+ from the intent?
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
How can i remove Google+ from the intent?
List<Intent> targetIntents = new ArrayList<Intent>();
Intent templateIntent = new Intent(Intent.ACTION_SEND);
templateIntent.setType("text/plain");
List<ResolveInfo> packages = this.getPackageManager().
queryIntentActivities(templateIntent, 0);
// remove desired package
for (ResolveInfo candidate : packages) {
String packageName = candidate.activityInfo.packageName;
if (!packageName.equals("com.google.android.apps.plus")) { //GooglePlus Package name
Intent target = new Intent(android.content.Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Your Text Here");
target.setPackage(packageName);
targetIntents.add(target);
}
}
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Share.");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
Its working properly in every API level, but not working properly on Lolipop(api 21)
Code is here:
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.google.com/"));
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
internetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(internetIntent);
Check this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com/"));
startActivity(intent);
Yes, it wont work in Lollipop, or may not work on any brand Manufactured(Sony,Samsung) phones, because you are trying to open an application with package name com.android.browser
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
In Lollipop default browser is chrome, which has package name something else. And there is no application with package com.android.browser in Lollipop.
Same thing can happen to any other Sony/Samsung phones.
What you can do is either call for a Application Chooser Dialog or find the default browser application, and open it
1) Code for Application chooser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
startActivity(intent);
2) Code to Find default app and launch it:
ComponentName cn=null;
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
ResolveInfo resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.activityInfo.packageName.equals("android")) {
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : resolveInfos) {
if (!info.activityInfo.packageName.equals("android")) {
cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
}
}
if(cn==null)
cn = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
} else {
cn = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
}
if(cn!=null){
Intent openIntent = new Intent();
openIntent.setComponent(cn);
openIntent.setData(Uri.parse("http://www.google.com/"));
startActivity(openIntent);
}
I need to share text from my applications via other social applications.I have got a piece of code that is working fine but it shows apps list in dialogue box:
And I want same feature in this way.May be I need to create views dynamically as per application installed in device.
Please suggest me how can I achieve this
This code will let you find all the apps available for the specified intent:
public static ArrayList<Availables> getAvailableAppsForIntent(Intent intent, Context context) {
ArrayList<Availables> availables = new ArrayList<Availables>();
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
for(ResolveInfo info : infos) {
ActivityInfo activityInfo = info.activityInfo;
IntentFilter filter = info.filter;
if (filter != null && filter.hasAction(intent.getAction())) {
// This activity resolves my Intent with the filter I'm looking for
String activityPackageName = activityInfo.packageName;
String activityName = activityInfo.loadLabel(manager).toString();
String activityFullName = activityInfo.name;
Drawable icon = activityInfo.loadIcon(manager);
Availables available = new Availables(activityName, activityFullName, activityPackageName, icon);
available.setForIntent(intent);
availables.add(available);
}
}
return availables;
}
Availables is just a class i created to store all the informations about the apps but you can manage it however you want.
then you can start the app this way:
Intent intent = available.getForIntent();
intent.setPackage(available.getAppPackage());
context.startActivity(intent);
Hope it helps!
hare is button click event use it
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
String shareBody = "Here is the share content body";
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(intent, "Share via"));
Write code on click of any View:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "Your Message Here...");
return intent;
i've a android app to share message to WeChat(without sdk).
When i directly use the StartChooser method, the display-name 'Send to Moment' and 'Send to Chat' display well.
But when i want to remove the apps i donot need using a intent filter as follows, there's problem that both display-name show 'WeChat' rather than 'Send to Moment' and 'Send to Chat'.But at the same time,their icon are right!
Who can tell me how to get the right display label?? Thank you!
Intent it = new Intent(Intent.ACTION_SEND);
it.setType("image/*");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo)
{
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("image/*");
ActivityInfo activityInfo = info.activityInfo;
if (activityInfo.packageName.contains("tencent.mm") || etc..)
{
targeted.setClassName(activityInfo.packageName, activityInfo.name);
targeted.setPackage(activityInfo.packageName);
targeted.putExtra(Intent.EXTRA_TEXT, "share text");
targeted.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
!!! update !!! :
if the wechat intent being placed in "Intent.createChooser", the label shown is right, but shown wrong label when placed in "EXTRA_INITIAL_INTENTS".
UPDATE2:
I find the answer at How to make an intent with multiple actions. Using LabeledIntent will solve the problem.Over.
Here is how I get it to work.
CharSequence label = info.loadLabel(getPackageManager());
Intent extraIntents = new LabeledIntent(targeted, activityInfo.packageName, label, info.icon);
targetedShareIntents.add(extraIntents);