简体   繁体   中英

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!

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.

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.

Have you gone step by step in the debugger to find exactly where the crash occurs?

One thing to look out for is if you are overriding any Android lifecycle events that you are properly calling the base class's constructor when necessary.

我认为你必须使用生命周期方法onPause()onResume()方法来取消注册和注册广播意图。

I'm not sure that you can understand what I want to say, because of my English.

I think this line of code is cause of a problem:

userInfo.setSessionString(sessionString); 

My project was wrong because I want to:

int i = Integer.parseint(intent.getExtars("9"));

and you register two times.

我发现确保意图是从原始活动运行并将事物放入一个类中解决了整个问题。

This is a very old question, but I think many people would still be searching answers for it. My situation is quite similar to this one.

What I wanted to do, is to call finish() in the child activity when certain events occur in parent activity ie by sending the broadcast message from MainActivity to Child Activity.

Following is the code in MainActivity when the event is occurred (eg when network connection is broken etc etc):

    Intent intent = new Intent("ACTIVITY_FINISH");
    intent.putExtra("FinishMsg","ACTIVITY_FINISH: Network broken.");
    sendBroadcast(intent);

In the child activity's OnResume() function:

    IntentFilter quitFilter = new IntentFilter();
    quitFilter.addAction("ACTIVITY_FINISH");
    registerReceiver(m_quitReceiver, quitFilter);

In the child activity's OnPause() function:

    unregisterReceiver(m_quitReceiver);

Within the child activity class:

BroadcastReceiver m_quitReceiver = new BroadcastReceiver() 
{
    public void onReceive(Context context, final Intent intent) 
    {
        if (intent.getAction().equalsIgnoreCase("ACTIVITY_FINISH"))
        {
            Log.d("INFO", intent.getExtras().getString("FinishMsg"));
            finish(); // do here whatever you want
        }
    }
};

Hope this helps.

when you are looking at error logs, you are just looking at first few lines. but surprisingly actual problem is mentioned in lines way below it-

Fatal Error : Main
error receiving broadcast...bla bla bla  <- you are just looking here only
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......
caused by : ........................... <- but actual problem is here!
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......

It might because you keep registering BroadcastReceiver. I made that mistake before as a result it returns this error. Make sure BroadcastReceiver only registered once.

Ok what worked for me was declaring the onNotification method as follows:

    window.onNotification = function(event) {
    alert('onNotification called!');
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM