I am building an app say Tx , which call's the createChooser using the below code.
Intent shareIntent = new Intent("com.myapplication.test");
shareIntent.putExtra(Intent.EXTRA_TITLE, "Sharing ..");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Shared from Tx app.." );
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent,"Choose the app ..")));
I have also built few app's with below in their androidManfiest.xml files.
<intent-filter>
<action android:name="com.myapplication.test" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
When Tx's createChooser is called, I am able to see all the app's where -com.myapplication.test is present & also the title as - Choose the app .. . However, I am not able to see the EXTRA_TITLE which is set to "Sharing ..".
Is there any way I can show this in the chooser? any help would be highly appreciated.
Related
It seems that Android automatically sets the title of sharing intents to the name of the app. I would like to override it to make it a little more descriptive (e.g. instead of "My App", the title could be "Preview with My App".
How can I change the title in the sharing dialogue pop up box in Android?
I have added the following code to my AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
The key is to start your intent as an activity by passing it as an argument to Intent.createChooser(intent: Intent)
Here's an example in Kotlin. We all love Kotlin dont we? :)
Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, "Some Text")
type = "text/plain"
startActivity(Intent.createChooser(this, "My Title"))
}
<intent-filter> supports the attributes android:icon and android:label. You can use them to change the icon and label that are displayed in the system's Share UI.
Please note that (at least on Android 10) the app name is still displayed. So instead of "Preview with My App" you maybe want to just use "Preview" as label.
Example:
<intent-filter
android:icon="#drawable/share_icon"
android:label="#string/share_label">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
See https://developer.android.com/guide/topics/manifest/intent-filter-element
The title has to come along with the Share Intent. So, if your application is one of the apps that can show a preview, then there is nothing you can do to show the title for choosing your app(it has to come from the calling app).
On the other hand, if you are using the Share Intent in your application itself, you can try this:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "What ever text you cant to share");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Preview with:")); <---- here
Check the description for createChooser
Extremely weird behaviour, I'm fairly new to Android, but I'm generating some json (first line below) and I'm trying to send it to an application whose manifest says it accepts anything with type "application/xctsk".
The generated file is fine, but the app chooser doesn't offer that application. I can still share the file to Whatsapp and it will be fine, content-wise. However, I still can't open it from Whatsapp.
Meanwhile I can
open the file manually
open other files from other sources, from Whatsapp
Receiving app's Manifest
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/xctsk"
android:host="*"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
<data android:scheme="content"/>
<data android:scheme="file"/>
</intent-filter>
My code
Uri xctskURI= saveTask(somejson);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, xctskURI);
shareIntent.setType("application/xctsk");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Send to XCTrack"));
The receiving app supports only ACTION_VIEW. You are using ACTION_SEND. These don't match. Either change your sending app to use ACTION_VIEW or change the receiving app so that it supports ACTION_SEND.
Turns out to be very strict, resolver was not satisfied with less that a perfect match.
ALL the categories had to be matched. And for some reasons, setting the MIME type erases the data.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_VIEW);
shareIntent.addCategory(Intent.CATEGORY_DEFAULT);
shareIntent.addCategory(Intent.CATEGORY_BROWSABLE);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setDataAndType(xctskURI,"application/xctsk");
startActivity(Intent.createChooser(shareIntent, "Send to XCTrack"));
What I want is to share the link of specific page of my application. Suppose I am in a profile page of some user in my app and I want to share that user to some of my friends so I would like to share it via WhatsApp and other applications.
So basically I want to share a link to my app.
This is what I am doing right now
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + activity.getPackageName());
activity.startActivity(Intent.createChooser(shareIntent, "Share"));
I know this is not what I really want but it's partially done.
if user hasn't installed my app then this code redirect him to google play store page to download this app.
Main problem is when user have already this app this link just opens the app. I want to redirect user to that specific page when he clicks on a link.
How can I do this?
I heard something about intent-filter but not sure how to use them.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
UPDATED
Now I am trying to share my app link like this
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra("PostID", postID);
shareIntent.putExtra("UserID", userID);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + activity.getPackageName());
activity.startActivity(Intent.createChooser(shareIntent, "Share"));
Inside my manifest I have declared an activity like this
<activity
android:name=".activities.ShareActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="details"
android:scheme="market" />
<data
android:host="play.google.com"
android:pathPattern="/store/apps/details?id=my_package_name"
android:scheme="http" />
<data
android:host="play.google.com"
android:pathPattern="/store/apps/details?id=my_package_name"
android:scheme="https" />
</intent-filter>
</activity>
But I am still not able to see my app when I click on the link I shared using WhatsApp or any other means.
Please help me
Any help will be really appreciated.
Thanks in advance.
Add the following intent-filters to your activity declaration, in the AndroidManifest of your project :
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
Now, in the onCreate(), onResume() or onNewIntent() method of your activity lifecycle, you can use the getData() of the intent that has fired your activity.
My aplication can generate CSV files that I want to share. I'm using MIME type text/comma_separated_values/csv, but when I send the Intent the chooser isn't shown, I guess my device doesn't know how to handle the file. Which type should I use?
This is my code:
Uri csv = lh.createDailyCSV();
if(csv == null){
Toast.makeText(this, getString(R.string.error_creating_csv), Toast.LENGTH_LONG).show();
}
else{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/comma_separated_values/csv");
sharingIntent.setData(csv);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.send_to)));
}
I declared in my manifest:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/comma_separated_values/csv" />
</intent-filter>
And I get the exception
03-12 12:19:23.430: E/ActivityThread(24011): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1#412fc920 that was originally registered here. Are you missing a call to unregisterReceiver()?
I've read this Exception occurs when there is none or just 1 option in the chooser.
[EDIT]
I changed how I attach the data to the Intent. Instead of sharingIntent.setData(csv) I used:
sharingIntent.putExtra(Intent.EXTRA_STREAM, csv);
And now the chooser works fine, but if I try to send the file via e-mail I get an error: File couldn't be shown.
[/EDIT]
The correct MIME type would be text/csv. If that doesn't work, you can use text/plain which will allow the user to potentially choose from a lot of apps, including Evernote etc.
Update After the update, it seems that you don't want to "share" the file with arbitrary other apps but only send it by e-mail? Please clarify.
In my case, the following works in Goo:
intent.setType("text/*");
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
<data android:mimeType="text/*"/>
</intent-filter>
In my application, I want to make a picker that offers the user the choice to pick a music. I want to use the native android picker. I used the following code to open the native android music picker:
final Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);
But when I execute it, I get an ActivityNotFoundException and this error message:
"Your phone has no music gallery that can be used to select a file. Please try sending a different type of files"
Am I doing something wrong there ?
This worked well for me:
public static final int REQ_PICK_AUDIO = 10001;
//------
Intent audio_picker_intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(audio_picker_intent, REQ_PICK_AUDIO);
The more general intent with Intent.ACTION_GET_CONTENT can present the user with a number of options for activities to pick an audio file (Astro file manager, etc.). However, the user could select any file, not necessarily an audio file. I wanted one that simply allowed the user to select an audio file from their media. This did the trick.
If you take a look at the AndroidManifest.xml file for the latest core Music app, it may shed some light on the options that you have. For example:
<activity android:name="com.android.music.MusicPicker"
android:label="#string/music_picker_title" android:exported="true" >
<!-- First way to invoke us: someone asks to get content of
any of the audio types we support. -->
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
</intent-filter>
<!-- Second way to invoke us: someone asks to pick an item from
some media Uri. -->
<intent-filter>
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="vnd.android.cursor.dir/audio"/>
</intent-filter>
</activity>
So based on this, you might first try
final Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);
and see if it fits your needs. You may also look at adding the category flags noted in the above example to help narrow down the results (e.g. OPENABLE should filter to only content that can be opened as a stream.
Something along the lines of this may work
// some Intent that points to whatever you like to play
Intent play = new Intent(Intent.ACTION_VIEW);
play.setData(Uri.fromFile(new File("/path/to/file")));
// create chooser for that intent
try {
Intent i = Intent.createChooser(play, "Play Music");
c.startActivity(i);
} catch(ActivityNotFoundException ex) {
// if no app handles it, do nothing
}