I register a BroadcastReceiver in my activity to handle broadcast that it send by a GCMBaseIntentService when a message is arrived.When my app runs in foregound everything works fine but after a long time in backround even if I dont unregister receiver my activity dont work properly(dont receive the broadcast).How can I resolve that?
Becasuse it is my first time using BroadcastReceiver this is my code:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="dim.taxigen.taxigen_driver"/>
</intent-filter>
</receiver>
In manifest.
registerReceiver(mreceiver, new IntentFilter(
"dim.taxigen.taxigen_driver.DISPLAY_MESSAGE"));
and how i register in my Activity.
I assume you don't declare your BroadcastReceiver in your manifest but programmatically. That is most likely your problem, because if your app is in the background it may get killed at any moment when memory gets low. The longer it is in the background, the bigger is the chance that it gets killed.
You can declare your BroadcastReceiver in xml like this:
<receiver android:name="com.example.Receiver" >
<intent-filter>
... // Put the broadcasts you want to receive here
</intent-filter>
</receiver>
Just put that in the application tag in your manifest.
First, if you register a BroadcastReceiver in your activity, when onStop() / onPause() gets called you should remove the broadcast, because if your app runs on background for a long period of time your Activity is likely to be destroyed.
If you want always your app to receive a broadcast, you should register a receiver object in your manifest.
Something like:
<receiver android:name="com.smm.receivers.NewMessageReceiver" >
<intent-filter>
<action android:name="com.smm.action.message_received" />
</intent-filter>
</receiver>
And then create an object called NewMessageReceiver in your application which will handle always your broadcast.
Hope it helps
Related
iam using MediaButtonReciever in my Streaming service to listen handle head sets and different devices action
i'm declaring it in Manifest like this
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
The actions works just fine as long as the app in the background
once the app is terminated if i clicked on any MediaButton it crashes as the media button with the following crash
Fatal Exception: java.lang.RuntimeException: Unable to start receiver android.support.v4.media.session.MediaButtonReceiver: java.lang.IllegalStateException: Could not find any Service that handles android.intent.action.MEDIA_BUTTON or a media browser service implementation
the problem is the receiver keeps receiving even if the app is destroyed, now how can i unregister the receiver once the app close ?
i have tried audioManager.unregister(MediaButtonReciever) but its depreciated
the problem was that i was using the default class in my manifest like this
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
which was wrong the the receiver automatically unregister after onReceive() is finished
Once you return from onReceive(), the BroadcastReceiver is no longer active, source
so all idid was i extened MediaRecieverButton in my custom class MyMediaButtonReceiver
and edited my manifest like this
<receiver android:name=".MyMediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
and it worked just fine
If you only want to handle buttons when your app is running, then you shouldn't declare this in the manifest. You should register and unregister the BroadcastReceiver dynamically in your code when you want it to respond to events.
To register the receiver, do:
BroadcastReceiver receiver = new MediaButtonReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
registerReceiver(receiver, filter);
When you are done (ie: in onDestroy() of your component), unregister the receiver like this:
unregisterReceiver(receiver);
in my case I got this error and I solve it by set enable flag of My service to true:
android:enabled="true"
<service
android:name=".MyService"
android:enabled="true"
android:multiprocess="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</service>
So here is the thing,i have an app,if the music is not playing(for which i am using service) and the user exits out of the activity then I want the service to stop and the app to stop too,which happens perfectly
Now i start the app again,everything works fine,except the widget stops receiving the broadcasts even though that same broadcasts is being received by another activity when it is supposed to.
Also strangely the widget is able to send broadcasts (it is only unable to receive it)
so it seems like the broadcastreceivers are not being registered again(only for the widget).How can i register it again??I really have to use system.exit(),not using it is screwing up other things :(
This is the what i am calling in ondestroy()
if(ma.boolMusicPlaying1==false)
{
getActivity().getApplicationContext().stopService(new Intent(this.getActivity(), Music_service.class));
ma.NP_List.clear();
ma = null;
System.gc();
System.exit(0);
}
So my guess is if i can register the widget through java file my problem would be solved.So can i register the widget through a java file??
in other words what is the java code for
receiver android:name="source.justanothermusicplayer.service.MyWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
I set up a simple app. I wan't to hide it from the drawer and I want to add a Boot Receiver to launch a service.
To hide the application, I read that I had to remove this from the manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
But when I remove it, the boot receiver doesn't work anymore.
I added the permission under the manifest tag
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and my receiver under application
<receiver android:name="com.example.receiver.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And in the receiver code, there is just a Toast
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Received", Toast.LENGTH_SHORT).show();
}
}
Why couldn't I set the boot receiver AND the hidden app from drawer?
Thanks
Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no BroadcastReceviers(ACTION_PACKAGE_INSTALLED, BOOT_COMPLETED etc.) will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)
This is an anti-malware move by Google. Google has advocated that users should launch an activity from the launcher first, before that application can go do much. Preventing BOOT_COMPLETED from being delivered until the activity is launched is a logical consequence of the that argument.
More details about this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-applications/
I am trying to stop a BroadcastReceiver from showing up once I exit my application. Until now I only made it show a Toast when an App is installed. It works really well, except that if I exit the app the receiver is still active.
This is my Receiver code from the AndroidManifest:
<receiver android:name=".MyBrowdcastreceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>
I want to know what should I put in my onDestroy() or onStop() methods from my main Activity that will cause the receiver to stop.
Thanks.
You have registered the receiver in manifest file which will receive notification even if your app is not running.You can do two things to fulfill your need:
You need to register , unregister the receiver programmatically.
You can take boolean which you will set true if the app is running else false. You can use this boolean on BraodcastReceiver's onReceive() method where you need to put check over the boolean and perform your action if the boolean is true.
I have following receiver that listens to Boot_Completed
<receiver android:name=".receivers.ActionBootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
I want to add another intent-filter with my own custom action. And make it private to my app if possible. This is mostly for code reuse so I can run same code path as when BOOT_COMPLETED.
So, I need following (if it's even possible)
1. intent-filter and make it private to my app
2. Code to send that intent so my receiver get's it.
Thanks!
Just use another <action> tag! It is all you need to register receiver for an particular intent.