Is there a way to restrict share options in an Android app? I have tried using the ShareActionProvider or just simply starting an intent using the Intent.ACTION_SEND intent option. Basically I want to be able to restrict sharing through email only or something of the sort.
you can use something like this, but instead of facebook look for a different name
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"this is a string");
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri); //Share the image on Facebook
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
break;
}
}
You can customize the intent chooser as per your need like this -
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject to be shared");
if (StringUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://link-to-be-shared.com");
}else{
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text message to shared");
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Hope this helps you.
Related
I have tried to send Email through Android Intent by using below code
Intent sendIntent = new Intent();
sendIntent.putExtra(Intent.EXTRA_TEXT, EmailContent);
sendIntent.putExtra(Intent.EXTRA_EMAIL, RecipientName );
sendIntent.putExtra(Intent.EXTRA_SUBJECT , subject );
sendIntent.setType("message/rfc822");
sendIntent.setAction(Intent.ACTION_SEND);
Intent chooser = Intent.createChooser(sendIntent , chooser_title );
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
In the email app the recipient details are not getting update whereas all the other details such as subject, body is getting updated with my input . Could you please suggest what needs to be done to resolve this .
Try to pass the EXTRA_EMAIL as string array.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Try using this:-
Create String resource for recipients:-
<string-array name="receipients">
<item>mgf#kgf.co</item>
<item>sdf#kgf.co</item>
</string-array>
and use this intent
Intent gmailIntent = new Intent(Intent.ACTION_SEND);
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, resources.getStringArray(R.array.receipients));
gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EmailContent);
gmailIntent.setType("message/rfc822");
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
final PackageManager pm = context.getApplicationContext().getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") ||
info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
if (best != null)
gmailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
try {
startActivity(gmailIntent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context.getApplicationContext(), getString(R.string.you_do_not_have_gmail_installed), Toast.LENGTH_SHORT).show();
}
how when clicked onclick button whatsapp messages can be sent automatically
public void onclick(){
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Uri uri = Uri.parse("smsto:" + "628124291xxxx");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "hallo");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
} else {
Toast.makeText(MainActivity.this, "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
}
}
I don't know if it's the way you want it. I believe it is a way of doing. Try this.
String toNumber = "628124291xxxx";
String url = "https://api.whatsapp.com/send?phone="+ toNumber;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.whatsapp"); //com.whatsapp or com.whatsapp.w4b
i.setData(Uri.parse(url));
startActivity(i);
I am using Google Vision OCR to grab the email from a business card (the OCR Graphic activity) and send it to the the To destination in the SendEmail activity. My log shows that the email text is detected.
I tried to set the intent to send it to the next activity, but I am getting two errors, "cannot resolve constructor Intent" on my new intent, and start activity cannot be applied to.
This is the OcrGraphic activity
List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
float left = translateX(elements.getBoundingBox().left);
float bottom = translateY(elements.getBoundingBox().bottom);
if (elements != null && elements.getValue() != null) {
if (elements.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*#\"\n" +
"\t\t+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$") || elements.getValue().contains("#")) {
Log.e("elementsemail", elements.getValue());
String email;
email = elements.getValue();
cEmail = email;
Intent sendIntent = new Intent(this, SendEmail.class);
sendIntent.putExtra(Intent.EXTRA_EMAIL, cEmail);
startActivity(sendIntent);
}
this is my Send Email activity
private void sendMail(){
Intent getIntent = getIntent();
String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);;
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
I want to send the email address to the SendEmail activity. I am new to java and android, any help is welcomed.
I think you're problem is how you obtain the extra(EXTRA_EMAIL)
Replace String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);; with String recipientList = getIntent.getStringExtra(Intent.EXTRA_EMAIL);
please replace this :
Intent sendIntent = new Intent(this, SendEmail.class);
whit this :
Intent sendIntent = new Intent(getApplicationContext(), SendEmail.class);
edit
You need to pass context in a constructor like this
private Context context;
OcrGraphic(GraphicOverlay overlay, TextBlock text, Context context) {
super(overlay);
this.context = context;
}
And then
Intent sendIntent = new Intent(context, SendEmail.class);
I'm working on a app-drawer replacement but I'm having trouble giving the user the ability to create home screen icons.
I can create the shortcuts, but I'm struggling to pull the icons from the respective apps.
I collect information on the apps using:
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i,0);
Collections.sort(availableActivities, new ResolveInfo.DisplayNameComparator(manager));
for(ResolveInfo ri:availableActivities){
AppDetail app = new AppDetail();
app.name = ri.activityInfo.packageName;
app.label = ri.loadLabel(manager);
app.icon = ri.activityInfo.loadIcon(manager);
app.iconID = ri.getIconResource();
apps.add(app);
But I'm running into troubles when I try to create the shortcut
AppDetail selectedApp = apps.get(info.position);
...
Intent shortcutIntent = new Intent(this, selectedApp.getClass());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, selectedApp.label);
// the danger zone
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getBaseContext(),selectedApp.iconID));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
finish();
How can I correctly reference the icon file of another app?
Please use
Drawable icon = manager.getApplicationIcon(app.name);
to obtain the icon graphics, and
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, selectedApp.label);
// this line in your code changed
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon.getBitmap());
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
If I only send text, the Share Intent chooser DOES NOT give Facebook/Twitter as an option.
Only Gmail, Skype and Evernote are options.
Here is my code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(shareIntent, "Share using"));
I have tried different combination of setType() with no joy including "text/*", "text/html" and passing HTML text in the putExtra as follows:
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
When I use "text/plain", Facebook comes up as an option but the text does not load up when selecting it. But the text does load for Twitter,email, SMS.
Has anyone else encountered this problem?
When I share an image, there is no problem and Facebook along with other social media apps are available on the list.
Also it should be "text/plain" and not "plain/text" according to the documentation.
this depends upon what intent filters are defined by each of those apps.
For instance if I add intent-filter
android.intent.action.send
If I choose single image from Gallery my application will appear in the list. However if I choose multiple my application will not appear as I have not added the intent-filer for android.intent.action.send_multiple
So it depends upon what intents facebook is filtering for. You need to see the release notes or help or developer pages for that.
The facebook issue is a limitation in facebook permissions. Use the facebook API
Share via Twitter :
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)v.getTag(R.drawable.ic_launcher));
// for finding twitter package name ---- >>
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
// finding facebook package name
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT(String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT(String)v.getTag(R.drawable.ic_launcher));
// finding gmail package name ---
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("gmail"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}