Android 错误接收广播意图问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1516211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Receiving Broadcast Intent Problem
提问by Niko Gamulin
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
10-04 13:30:43.218: 错误/AndroidRuntime(695): java.lang.RuntimeException: 在 com.client 中接收广播 Intent { action=com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE (has extras) } 时出错。 gaitlink.GaitLink$LoginStatusReceiver@431690e8
The broadcast message is sent from CommunicationService class in the following method:
广播消息是从 CommunicationService 类通过以下方法发送的:
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:
并在 onResume 方法中注册:
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!
谢谢!
采纳答案by JRL
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.
需要注意的一件事是,如果您要覆盖任何 Android 生命周期事件,您是否在必要时正确调用了基类的构造函数。
回答by shencp
I solved it by adding intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
when you start a new activity
.
我通过intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
在你开始一个新的activity
.
If not started from an activity
, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
is needed.
如果不是从 开始activity
,intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
则需要。
回答by CommonsWare
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.
你有两个Intent
过滤器;你只需要一个。如果您注册BroadcastReceiver
via registerReceiver()
,则仅使用该IntentFilter
API 调用的第二个参数 - 不要同时将<intent-filter>
元素放入<activity>
清单中的 。
I do not know for certain if that is your problem, but it certainly does not help.
我不确定这是否是您的问题,但这肯定无济于事。
回答by aquadeep
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 i.e. by sending the broadcast message from MainActivity to Child Activity.
我想要做的是,当父活动中发生某些事件时,即通过将广播消息从 MainActivity 发送到子活动,在子活动中调用 Finish()。
Following is the code in MainActivity when the event is occurred (e.g. when network connection is broken etc etc):
以下是事件发生时 MainActivity 中的代码(例如,网络连接中断等):
Intent intent = new Intent("ACTIVITY_FINISH");
intent.putExtra("FinishMsg","ACTIVITY_FINISH: Network broken.");
sendBroadcast(intent);
In the child activity's OnResume() function:
在子活动的 OnResume() 函数中:
IntentFilter quitFilter = new IntentFilter();
quitFilter.addAction("ACTIVITY_FINISH");
registerReceiver(m_quitReceiver, quitFilter);
In the child activity's OnPause() function:
在子活动的 OnPause() 函数中:
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.
希望这可以帮助。
回答by user1008590
i think you have to use life cycle method onPause()
and onResume()
Method for unregister and register broadcast intent.
我认为您必须使用生命周期方法 onPause()
和onResume()
方法来取消注册和注册广播意图。
回答by zhuizhu
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.
然后你注册了两次。
回答by msj121
I found that making sure the intent was run from the original activity and putting things into one class got rid of the entire issue.
我发现确保意图从原始活动中运行并将事物放入一个类中可以解决整个问题。
回答by Fabien Thetis
Ok what worked for me was declaring the onNotification
method as follows:
好的,对我有用的是声明onNotification
方法如下:
window.onNotification = function(event) {
alert('onNotification called!');
};