Her is what I'm trying to accomplish:
- When user tries to share some text from any app (Like sharing a tweet or a link), my app will appear in the sharing list.
- If he select my app, some simple code will be run (like showing a Toast) then that's it. No interface or UI is needed.
Here is how I did it:
AndroidManifest.xml
<receiver
android:name=".MyBroadcastReceiver" >
<intent-filter
android:label="select my app">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</receiver>
MyBroadcastReceiver.java
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//Some simple code
Toast.makeText(arg0, "My Receiver is cool", Toast.LENGTH_LONG).show();
}
This didn't work, my app doesn't show in the sharing list.
Why I work with BroadcatReceivers
and not with Activities
? Because I don't want any UI and I think that is why we have receivers (correct my plz)
Am I doing it right?
ACTION_SEND
is used with activities. You are attempting to use aBroadcastReceiver
. Since aBroadcastReceiver
is not an activity, this will not work.No.
You still need an activity. However, if you give the activity
Theme.NoDisplay
, and you do not callsetContentView()
, there will be no UI for the activity. Usually, you just callfinish()
fromonCreate()
after doing whatever it is you wanted to do (in this case, display aToast
).For example, this sample application shows an activity (
FauxSender
) set up to do precisely what you want: respond toACTION_SEND
with aToast
. It has another activity that issues anACTION_SEND
so you can see the results.