I read the documentation on receiving simple data. I want to receive an URL,i.e. text/plain from other apps.
So, I declared only this intent filter:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
In my MainActivity.class:
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
I'm handling the received text as:
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
textBox.setText(sharedText);
}
}
But, the document reads that I should handle MIME types of other types carefully.
1) But, since I only registered for plain/text, do I need any more type handling code?
2) Moreover, citing documentation :
Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the intent.
The MainActivity.java is also started by LAUNCHER. How do I handle that?
3) Once, user selects my app from the dialog, does it open that app, in all cases? I don't need my app to be opened. Can I circumvent that?
EDIT : I don't need my app's UI to be opened. But I want to receive the text.
1. Yes, you need to add mimeType
s for all file types you wish to share.
2. I think the problem may be that a starting Activity
will also have
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />.
in its manifest declaration. So when you call
intent.getAction()
which action is returned ?? ACTION_SEND
or MAIN
? This is the problem they are talking about, I believe.
3. If you don't want your app to be displayed in the list of share apps, then why have you added the action
<action android:name="android.intent.action.SEND" />
to this Activity
in the manifest in the first place ?? Because, the purpose of adding an action to an intent-filter is precisely that the Activity
or Service
or BroadcastReceiver
can be started from another app by sending an implicit intent. And if you don't want that to happen, then how do you plan to "share text" anyway ??