Error Receiving Broadcast Intent Problem - android

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.

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......

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.

i think you have to use life cycle method onPause() and onResume() Method for unregister and register broadcast intent.

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.

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.
Following is the code in MainActivity when the event is occurred (e.g. 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.

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!');
};

Related

Android Intent reopening activity

I have an issue that I have not found a solution to on this site, but if this is a duplicate question, I apologize.
I am developing an application that serves as a terminal for registering when employees start/finish work, among numerous other things. The way it works is that with NFC switched-on, they scan their NFC cards and my app reads them and ultimately sends the appropriate information to the server.
However, if the app is already open (it's supposed to be open all the time, so this is an issue) and an NFC card is scanned, it reopens the app. Of course, this is done because I have set it that way in the manifest. But I can not find a way to have my app recieve the NFC scan intent if I do not add all of these lines in the manifest:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I have tried just writing without the but in that case it does not read the card, but instead the program chooser comes up on the phone, or if the phone does not have an appropriate app it simply says "NFC read error".
Does anyone have a solution for this? This is the last step in my project, and I have had a lot of trouble with it, and would appreciate any help. It's probably something simple that I'm just not seeing, but I'd appreciate it either way.
Android activities have different launch modes. If you set single instance it will use already opened activity and doesn't create a new activity. You can read the new intent in override method onNewIntent()
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// read intent values here
}
For various activity elements
You can use broadcastReceiver,
- first initiate the receiver to your activity
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("whateveryouwant");
notificationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// here you can read the intent and customize the action;
int usage = intent.getIntExtra("usage",1000);
}
}
};
second register the broadcast
registerReceiver(notificationBroadcastReceiver,intentFilter);
At end unregister to the broadcast in the onDestroy method
#Override
protected void onDestroy() {
if(notificationBroadcastReceiver != null){
unregisterReceiver(notificationBroadcastReceiver);
notificationBroadcastReceiver = null;
}
super.onDestroy();
}
after doing that instead of intenting activity you can sendBroadcast()
a little guide: https://developer.android.com/reference/android/content/BroadcastReceiver.html
hope it will be helpfull

Custom intent to other app

I have a simple question but i can't find anything on google, maybe i use the wrong key word.
I'm developping an app with a service in background. This service is always started. I have a phone with a custom button than can start an app. But i want to use this button to start an action on my service don't start any activity.
To do that, i have think about an ugly solution : I configure my custom button to start an other app. This app is a blanck activity and on the onCreate() event i just send an itent to my service and after finish the activity.
My question is how can i send a custom intent to an other app ?
my idea : in the blanck activity write this
#Override
public void onCreate(){
super.onCreate();
Intent customIntent = new Intent("com.customIntent.action");
startActivity(customIntent);
finish();
}
On my service doing something like that :
IntentFilter it = new IntentFilter();
it.addAction("com.customIntent.action");
registerReceiver(myReceiver, it);
Thanks for your help !
You are registering receiver and it will only be catched when sendBroadCast will be called with the intent. Secondly, you are starting Activity with that Intent action. There is no Activity in xml/code which handles this action. Thirdly, you can add this Intent Filter in AndroidManifest against specific reciever and in Activity use
Intent customIntent = new Intent("com.customIntent.action");
LocalBroadcastManager.getInstance(this).sendBroadcast(customIntent);
AndroidManifest.xml
<receiver android:name="." >
<intent-filter>
<action android:name="com.customIntent.action" />
</intent-filter>
</receiver>
Hope this helps.
I dont't know if I understand your question correctly but this tutorial explains very well how you use an intent to interact with another app:
http://developer.android.com/training/basics/intents/index.html

onCreate of android service not called

I want to start a service in a static way. So from my activity I call
SpeechActivationService.makeStartServiceIntent(
this.getApplicationContext(),
"WordActivator");
Here is the actual class that extends from service class http://dpaste.com/hold/928115/ As you can see there are several log points, e.g. in the onCreate method.
This is not logged. Only if I put log text in makeStartServiceIntent method it appears, however not in the onCreate method.
Here's the makeStartServiceIntent method:
public static Intent makeStartServiceIntent(Context context,
String activationType) {
Intent i = new Intent(context, SpeechActivationService.class);
i.putExtra(ACTIVATION_TYPE_INTENT_KEY, activationType);
return i;
}
In manifest file I have
<service android:name="root.gast.speech.activation.SpeechActivationService"/>
Any ideas why the service is not started?
Aside from you not posting code showing startService(), it looks like the package name of your Service in the manifest doesn't match your SpeechActivationService class (assuming the code link you posted is the actual SpeechActivationService class in your project, and not just a class you copied from).
<service android:name="com.mkyong.android.SpeechActivationService"/>
Your makeStartService() just creates an Intent for you. You don't seem to actually be firing that intent off to start the service. Try like this
Intent i = SpeechActivationService.makeStartServiceIntent(this,"WordActivator");
startService(i);
Note that if this.getApplicationContext() works you are likely already inside of a Context object so simply using this should work also.

Application crashing when starting an activity from a broadcast receiver, sent from a service

First of all, I have already tried adding flag_activity_new_task, still its not working.
Here is the flow:
I have a service running that does http request on a background thread and fetches data. Whenever certain data is received, I need to display it to user(for example, an update to the app is available). For this, I use broadcast, and have setup a receiver. Everything works fine till here.
But, when I tru starting an activity from this receiver, app crashes saying:
"01-15 17:03:30.129: E/AndroidRuntime(28014): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
"
Even though I have added this flag. Here are the codes:
Service part:
try
{
JSONObject mAdDetails = mResult.getJSONObject(Tags.TAG_RPC_SMALL_AD_DETAILS);
Intent mI = new Intent();
mI.setAction(ReceiverAdvertSmall.INTENT_ACTION);
mI.putExtra(AppMapKeys.KEY_AD_HEADING, mAdDetails.getString(Tags.TAG_RPC_AD_HEADING));
mI.putExtra(AppMapKeys.KEY_AD_DESCRIPTION, mAdDetails.getString(Tags.TAG_RPC_AD_DESCRIPTION));
mI.putExtra(AppMapKeys.KEY_AD_LINK, mAdDetails.getString(Tags.TAG_RPC_AD_LINK));
sendBroadcast(mI);
}
catch(JSONException e)
{
}
The receiver part:
#Override
public void onReceive(Context mContext, Intent data)
{
Intent mI = new Intent(mContext, ActivityAdvertSmall.class);
mI.putExtras(data.getExtras());
mI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(data);
}
What Am I Doing Wrong? Iam unable to figure it out for last one hour.!!
Thanx in advance...
use
mContext.startActivity(mI);
instead of
mContext.startActivity(data);
for start Activity from onReceive because you are adding FLAG_ACTIVITY_NEW_TASK flag to mI intent but trying to start Activity using data intent
You should pass the intent you created to startActivity()
mContext.startActivity(mI);
Ok....here was the stupidiest mistake I made. I wrongly used startActivity(data) instead of startActivity(mI). Closing the question.
Just pass the intent you created to startActivity()
mContext.startActivity(mI);

How to startActivity() from BroadcastReceiver without recreating the activity?

I need to receive a broadcast event inside a broadcast receiver and than pass this information to an activity that is already open. How can I inform the activity from the broadcast receiver without it getting recreated? This causes a total refresh which is not needed.
Now I could receive intent inside a broadcast receiver that is declared within the activity, but I also need to receive the intent when its in background as well, hence the main place I am processing the intents is in a separate broadcast receiver. So I just don't know how to inform the activity that a new intent has arrived without onCreate() getting called and re-init the whole UI.
I think I need the NEW_TASK flag or it won't run.
PS: What are these insane downvotes about. What could be more relevant than how to start an activity from a broadcast receiver in such a way as not to recreate the activity. BTW, I am going to find an answer w/wo you. Why the bitter downvotes? I suspect it is because you know I could use an answer. Well I'll probably be posting an answer to this great question myself quite soon.
if you want to recreate this activity do this:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if don't want to recreate , just do this:
while this activity on your task's top,intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
declare the actvity in the manifest with: android:lauchMode= "singleTask";
And the answer is .....
declare the Activity in the manifest with
android:launchMode="singleTop"
Still would like feedback on this answer as I notice that in onResume() the intent does not seem to carry over the values as it did before. So I cannot pull out values that I set on the intent inside the broadcast receiver ...
Update: In order to get the values from the receiver you may need to do the following inside the Activity:
#Override
public void onNewIntent(Intent intent)
{
setIntent(intent);
}

Categories

Resources