Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Receiving Broadcast Intent Problem

I created a broadcast receiver in the main activity and the background service which is sending broadcast intents. The application crashes each time I try to run it and the Log displays the following error message:

10-04 13:30:43.218: ERROR/AndroidRuntime(695): java.lang.RuntimeException: Error receiving broadcast Intent { action=com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE (has extras) } in com.client.gaitlink.GaitLink$LoginStatusReceiver@431690e8

The broadcast message is sent from CommunicationService class in the following method:

private void announceLoginStatus(){
    Intent intent = new Intent(LOGIN_STATUS_UPDATE);
    intent.putExtra(SERVER_MESSAGE, mServerResponseMessage);
    intent.putExtra(SESSION_STRING, mSessionString);
    sendBroadcast(intent);
}

where

String LOGIN_STATUS_UPDATE = "com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE"

in the main activity the following broadcast reveiver is defined:

public class LoginStatusReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            String serverMessage = intent.getStringExtra(CommunicationService.SERVER_MESSAGE);
            String sessionString = intent.getStringExtra(CommunicationService.SESSION_STRING);

            userInfo.setSessionString(sessionString);
            saveSettings();
        }
    }

and registered in onResume method:

        IntentFilter loginStatusFilter;
        loginStatusFilter = new IntentFilter(CommunicationService.LOGIN_STATUS_UPDATE);
        loginStatusReceiver = new LoginStatusReceiver();
        registerReceiver(loginStatusReceiver, loginStatusFilter);

And the manifest file includes the following:

<activity android:name=".GaitLink"
              android:label="@string/app_name">
        <intent-filter>
            ...
            <action android:name="com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE" />
        </intent-filter>
    </activity>

I would really appreciate if anyone could explain why the Log displays the message above and the application crashes.

Thanks!

like image 859
Niko Gamulin Avatar asked Oct 04 '09 11:10

Niko Gamulin


People also ask

What are receiving and broadcasting intents in Android?

Broadcast intents are a mechanism by which an intent can be issued for consumption by multiple components on an Android system. Broadcasts are detected by registering a Broadcast Receiver which, in turn, is configured to listen for intents that match particular action strings.

How do I start a broadcast intent?

Sending Broadcast intents from the ActivitysetAction("com. journaldev. CUSTOM_INTENT"); sendBroadcast(intent); Don't forget to add the above action in the intent filter tag of the manifest or programmatically.

What are broadcast intents?

The Android system automatically sends broadcasts when various system events occur, such as when the system switches in and out of airplane mode. The system sends these broadcasts to all apps that are subscribed to receive the event.

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.


2 Answers

I solved it by adding intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); when you start a new activity.

If not started from an activity, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is needed.

like image 119
shencp Avatar answered Sep 29 '22 19:09

shencp


You have two Intent filters; you only need one. If you register the BroadcastReceiver via registerReceiver(), only use the IntentFilter that is the second parameter to that API call -- do not also put an <intent-filter> element in the <activity> in the manifest.

I do not know for certain if that is your problem, but it certainly does not help.

like image 27
CommonsWare Avatar answered Sep 29 '22 19:09

CommonsWare