29

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

Swati Garg
  • 995
  • 1
  • 10
  • 21
Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
  • I now realize that Activities are GUI only and that is their purpose so I will have to use a Service or something else. Thank you, Isaac – Isaac Waller Feb 08 '09 at 23:44

4 Answers4

98

Echoing previous response, you shouldn't use a broadcast receiver.

In the same situation, what I did was to declare the theme thusly:

<activity android:name="MyActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoDisplay">
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
JoeHz
  • 2,136
  • 1
  • 18
  • 29
  • 3
    This answer should have more upvotes, because it answers the exact question that was asked. The other answers are very true, but this was actually very helpful and answers the question that was asked. – prolink007 Mar 21 '13 at 17:21
  • 5
    Alas, I answered it well after it was posted, which probably explains why that didn't happen. Got me a Necromancer badge out of the deal, as well as your comment which made me smile :) – JoeHz May 12 '13 at 07:50
  • 1
    This is the ABSOLUTE right answer since there are things you can't do with a BroadcastReceiver. I currently ran into an issue, using USB devices. Using an Broadcast receiver instead of an activity make the app loosing permission everytime you disconnect! – blender Jun 10 '15 at 10:06
  • 1
    Just be aware of [this regression](https://commonsware.com/blog/2015/11/02/psa-android-6p0-theme.nodisplay-regression.html) on Android 6+. The "fix" is to use `@android:style/Theme.Translucent.NoTitleBar` instead. – ubuntudroid Oct 03 '16 at 19:50
  • More like the fix according to [this link](http://plus.google.com/+DianneHackborn/posts/LjnRzJKWPG‌​W) is to actually *finish* a NoDisplay using Activity when it is done doing its (what should be VERY short) thing. This seems to be kind of a no brainer. Apparently Google changed the behavior of Activities that used Theme.NoDisplay because people were complaining their apps ANRing when they didn't do that (Preventing Display for too long? What do you expect Android to do to your thing?) Before this misbehavior was tolerated by Android, but I'm surprised it did. – JoeHz Oct 04 '16 at 02:47
19

Your 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> 
Pratik
  • 30,639
  • 18
  • 84
  • 159
Reto Meier
  • 96,655
  • 18
  • 100
  • 72
  • please tell me what is MY_INTENT in _intent.getAction().equals(MY_INTENT), sorry if i sound naive, i'm just a beginner. – Sumit M Asok Feb 18 '10 at 12:33
  • You can use any String you want tu identify your intent. It has to be unique. I suggest you to use "your.package.name.START_SERVICE" – Guido Aug 17 '10 at 18:28
4

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.

lukejduncan
  • 121
  • 4
3

Use Service. I works definitely. When you click the program, it would do its work without any GUI. Use pendintgintent...getService(MySerice.class....). Then, create a new class MyService extending the Service class. Inside MyService.class, override onStart() and do whatever you want to do.

Manoj
  • 31
  • 1