Android Abort BroadcastReceiver - android

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.

Related

How to unregister MediaButtonReceiver onDestroy?

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>

Why my BroadcastReceiver is geting unregisted when my app runs in background?

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

android: how to trigger a service globally by special action?

I want to write a screen capture app, but I don't know how to trigger my app globally by special action such as shaking the device or long press a button or anything else. Which means bring up my app under any app any time. Is there any suggestion?
You'll need to use a reciever for that. For example, if you wanted the app to start activity MyReciever when the phone boots up, you could do the following:
<receiver android:name=".MyReceiver">
<intent-filter >
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

android BOOT_COMPLETED not received if not activity intent-filter

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/

android api auto startup activity after shutdown and restart

i have following problem:
i write broadcastReceiver that receive android.intent.action.BOOT_COMPLETED all work fine if i do restart from power options or remove battery, but if i use POWER OFF - power option system did't go thrue BOOT_COMPLETED intent. Please help what kind of intent i must receive to get this to cases for auto startup activity on android phone.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".StartupBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
In your Broadcast Receiver class, Check :
getIntent().getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED") {
// call startActivity(intent);
}
It will definitely help you.

Categories

Resources