Hello,
I have created a activity that is only meant to be launched from a link (using a intent filter.) I do not want this activity to have a GUI - I just want it to start a service and put a notification in the bar. I have tried to put the intent filter for the link in my service, but that does not work. Is there a better thing to do this that will answer to intent filters - or can I just make my activity not have a GUI?
Sorry if I'm being confusing, Isaac
views:
1793answers:
2Your best bet would seem to be using a BroadcastReceiver
. You can create a new BroadcastReceiver that listens for the Intent to trigger your notification and start your service like this:
public class MyIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Broadcast a notification
_context.startService(new Intent(_context, MyService.class));
}
}
}
And you can register this IntentReceiver directly in the application Manifest without needing to include it within an Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.myapplication"
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:enabled="true" android:name="MyService"></service>
<receiver android:enabled="true" android:name="MyIntentReceiver">
<intent-filter>
<action android:name="MY_INTENT" />
</intent-filter>
</receiver>
</application>
</manifest>
I'm not sure if a service would work, but a broadcast receiver definitely would not. Url's are launched using startActivity(). Broadcast receivers cannot respond to this.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
FTA: Note that, although the Intent class is used for sending and receiving these broadcasts, the Intent broadcast mechanism here is completely separate from Intents that are used to start Activities with Context.startActivity(). There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); likewise, when you broadcast an Intent, you will never find or start an Activity.