I have declared an activity and a broadcast receiver in my manifest file, however, I have the code for onReceive() in my activity as a separate public class. When I try to trigger a broadcast from the adb command line I get a classnotfound error.
My question is, is it mandatory to have the broadcastreceiver as a separate class in a separate file ?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityAsDialogActivity.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
:
:
}
public class TestEmail extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MYINTENT)){
Log.d("Manju ==>","ActivityAsDialogActivity, Got the intent"+MYINTENT);
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
}//end of if statment
}//end of onReceiver
}//end of Broadcast
}//end of class ActivityAsDialogActivity
below is manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mot.activityasdialog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityAsDialogActivity"
android:excludeFromRecents="true"
android:theme="@style/EmptyActivity"
android:configChanges="keyboardHidden|orientation|screenSize|uiMode">
<!-- android:theme="@android:style/Theme.Holo.Dialog"
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="com.mot.activityasdialog.ActivityAsDialogActivity.TestEmail" android:enabled="true">
<intent-filter>
<action android:name="com.mot.MANJU"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Short answer: Yes it needs to be a different Class. And in a separate file.
Long answer: Since your BroadcastReceiver must be a subclass of BroadcastReceiver and your Activity must be a subclass of Activity and Java does not allow multiple inheritance and Activity and BroadcastReceiver are not related in a suitable fashion. Yes, it has to be two classes.
It seems to have to be in its own file too. I tried it and the system cannot find the BroadcastReceiver if it is an inner class. public or not.