Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a broadcast receiver as an inner class in manifest file? [duplicate]

I want to create a broadcast receiver as an inner class in my main activity. But I have problems defining the broadcast receiver in manifest xml file, because android can't find it.

Code:

public class MyActivity extends Activity{     ...      public class Receiver extends BroadcastReceiver{          @Override         public void onReceive(Context context, Intent intent) {             ....         }      }      ... } 

Manifest:

<receiver android:name=".org.danizmax.myapp.MyActivity$Receiver" android:enabled="true">             <intent-filter>                 <action android:name="org.danizmax.myapp.BROADCAST_INITIAL_DATA"></action>             </intent-filter> </receiver> 

I tried with:

  • .org.danizmax.myapp.MyActivity$Receiver
  • org.danizmax.myapp.MyActivity$Receiver
  • .MyActivity$Receiver
  • .Receiver

I saw others also having similar problems, but did not find any answers.

So is it possible? If not, what's better way to use broadcast receivers?

Thanks!

like image 253
danizmax Avatar asked Dec 08 '10 20:12

danizmax


People also ask

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.

What is broadcast receiver how it is implemented?

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter. public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.


1 Answers

Yes, it is possible.

The receiver android:name attribute should look like .path.to.class.MyClass$MyInnerClass

like image 155
Cheryl Simon Avatar answered Sep 21 '22 02:09

Cheryl Simon