views:

76

answers:

3

Hi, I have an Activity which also has t olisten to a specific broadcast from a self-made service. I have noticed though that when I add that as an action to my Androidmanifest file the app won't actually start whilst debugging. The Activity is also my MAIN activity :

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

But when I add my own extra action to it it simply wont pick up the broadcast and it won't start up out of itself whilst debugging ????

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
   <action android:name="development.android.service.musicServiceUpdate">
</intent-filter>

Any idea whats wrong here or why my activity wont be catching the broadcast ? Is it possible to have two action's specified to a .MAIN action ?

/edit :

Tried the following :

<activity android:name=".nowPlayingGUI"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                    android:launchMode="singleTask">    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".nowPlayingGUI">
            <intent-filter>
                <action android:name="development.android.service.musicServiceUpdate"></action>
            </intent-filter>
        </receiver>

That won't work and will throw a :

10-14 11:57:04.412: ERROR/AndroidRuntime(11947): java.lang.RuntimeException: Unable to instantiate receiver development.android.myPlayer.nowPlayingGUI: java.lang.ClassCastException: development.android.myplayer.nowPlayingGUI

10-14 11:57:04.412: ERROR/AndroidRuntime(11947): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2520)

A: 

You can only have one action per intent filter (but you can have multiple categories). Add an additional intent filter instead.

Marloke
should it have a category ? or is an action enough ?
TiGer
If you want to add an additional activity, you should at least add the "default" tag. <category android:name="android.intent.category.DEFAULT" /> . If all you're wanting to do register a broadcast receiver, the answer above this one has what you're looking for.
Marloke
Actually it's exactly the same activity, which should be both the Main and also a Receiver...
TiGer
btw google's example :http://developer.android.com/guide/topics/intents/intents-filters.htmlactually shows one intent-filter with like 3 actions !!!
TiGer
I stand corrected. I believe you still need to separate out the receiver intent if you want to list it in the manifest though.
Marloke
A: 

Have you added the tag in your androidmanifest.xml

Assuming you have an activity and a broadcast receiver, I think your manifest xml would contain something similar to this inside your app tag:

    <activity android:name=".Main"
              android:label="@string/app_name">
        <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name =".myreceiver">
           <intent-filter>
              <action android:name=development.android.service.musicServiceUpdate />
         </intent-filter>
    </receiver>
kiki
ok but my receiver is also the .Main class that you are suggesting...
TiGer
ok, tried this, now when the broadcast should be received by the same Activity it will throw a 10-14 11:57:04.412: ERROR/AndroidRuntime(11947): java.lang.RuntimeException: Unable to instantiate receiver development.android.myPlayer.nowPlayingGUI: java.lang.ClassCastException: development.android.myplayer.nowPlayingGUI10-14 11:57:04.412: ERROR/AndroidRuntime(11947): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2520)
TiGer
@TiGer: I think you need to have different classes for each Activity, Service, Broadcast Receiver and Content Provider. And you have to mention in the manifest file how many activities, receiver, providers or services are there in your app.
kiki
wat a sec... an activity should be able to be a broadcast receiver as well right ?
TiGer
No no....the 4 different building blocks or components of an android application are Activity, Broadcast Receiver, Content provider and Service. You cant tangle them up. You can create a receiver and register the receiver with your activity.
kiki
this can't be : I actually have used an activity before with an intent-filter.. That same activity uses a BroadcastReceiver object to react on that intent-filter...So i has been declared as an Activity within the AndroidManifest but it also uses a BroadcastReceiver... That one actually worked simply because it hadn't another intent-filter for the "android.intent.action.MAIN".So there is no way for me to make my "android.intent.action.MAIN" also react on another intent-filter -> action ?
TiGer
just as added info : so it's impossible to create an app with a Single Activity (which get fired up at startup) and a Service and the Activity also reacting on messages send by the Service through Broadcasts ? That's simply not possible with Android ?
TiGer
You can both start a service and create a broadcast receiver from inside your activity. There are a number of tutorials floating around both on services and on broadcast receivers.
Marloke
Yup, that I already got working ;)What I am asking if that can be done withint one activity which is also the only activity in your application AND it'a also the activity that gets started at startup ?
TiGer
You can instantiate and register a broadcast receiver inside your main activity and set up the filters for that receiver within that same activity (if that's what you're aiming at). In this case you would not list the broadcast receiver's filter in the manifest. Instead use registerReceiver (where you set the intent filter) and unregisterReceiver inside the main activity. If you're looking for something different than that please be more specific.
Marloke