Current Behavior: The messages are being broadcast, but the receiver never gets them. I know this because the print statement in the broadcaster is printed, but the print statement in the receiver is never printed. What am I missing/doing incorrectly?
Here's the snippet where I tried (multiple ways) to broadcast a message, which is inside a class called IMService
that extends Service
:
//NEW_FRIEND_REQUEST is a String
Log.i( "MY_TAG", "broadcasting received friend request");
Intent i = new Intent( NEW_FRIEND_REQUEST, null, null, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( NEW_FRIEND_REQUEST );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( this, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
The broadcast receiver (which is a standalone class aka not an inner class):
public class NotificationReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("MY_TAG", "friends broadcast receiver received a friend request message");
}
}
AndroidManifest.xml:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="NotificationsReceiver" >
<intent-filter>
<action android:name="IMService.NEW_FRIEND_REQUEST" />
</intent-filter>
</receiver>
<activity
android:name="activities.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="activities.Login"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name="activities.Register"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_register" >
</activity>
<activity
android:name=".HttpRequest"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_signin" >
</activity>
<activity
android:name=".Messaging"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_messaging" >
</activity>
<service android:name=".IMService" />
<activity
android:name=".FriendList"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_friend_list" >
</activity>
<activity
android:name="activities.Home"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_home"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.Friends"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_friends"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.FriendRequests"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_friend_requests" >
</activity>
<activity
android:name="activities.PrivateChat"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_private_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.RandomChat"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_random_chat" >
</activity>
<activity
android:name="activities.Groups"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_groups" >
</activity>
<activity
android:name="activities.GroupChat"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_group_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.UserProfile"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_user_profile" >
</activity>
<activity
android:name=".Messages"
android:label="@string/title_activity_messages" >
</activity>
</application>
</manifest>
EDIT: After reading Mike's comment, I tried the following 2 things (separately), but the receiver still does not receive the message:
<!-- no filter -->
<receiver android:name="NotificationReceiver" />
and:
<!-- with a filter, and I changed the value of NEW_FRIEND_REQUEST to
public static final String NEW_FRIEND_REQUEST = "new_friend_request"; -->
<receiver android:name="NotificationReceiver" >
<intent-filter>
<action android:name="new_friend_request" />
</intent-filter>
</receiver>
Here is my project's hierarchy:
Last Edit:
For anyone curious about the exact warning when I used a capital letter for the package name, here's a screen shot:
When using implicit Intents - ie, Intents not created for a specific class - the action you're filtering for on the BroadcastReceiver must exactly match that which you use to create the Intent. In this case, the NEW_FRIEND_REQUEST
String specified in the code did not match the <action>
element in the manifest.
Also, when a Receiver is incorrectly listed in the manifest, firing an implicit Intent meant for it will fail silently, as the system won't be able find it. In this case, an extra s
in NotificationReceiver
was one problem. Another was that the Receiver is located in a subdirectory of /src
, and therefore in a different package. This was corrected by changing the name listed in the manifest to include the subdirectory in the package name.
Finally, it appears that capital letters in package names - and, therefore, project directory names - can cause problems, and should be avoided.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.