I need to add an action to an IntentFilter for detecting changes in the mobile-network.. so basically what I need is an equivalent of WifiManager.SUPPLICANT_STATE_CHANGED_ACTION but for the MobileNetwork. but I'm not really sure what the class I'm looking for is called. Any ideas?
Seems ConnectivityManager would help. It has some description in the documentation. Briefly, I think You need to:
Register BroadcastReceiver with ConnectivityManager.CONNECTIVITY_ACTION
Check changed network(or all connections)
Related
I am trying to understand how broadcast receiver works internally.
What happens internally when I call registerReceiver() method? Does it create a service internally ?
What happens internally when I call sendBroadcast?
If I have to implement my own BroadcastReceiver class (without extending the Android BroadcastReceiver class) how can I achieve it?
I did a lot of research, but I only found how BroadcastReceiver works. I looked at the Android source code too to find out how it works, but it didn't help either.
You wrote that you need to implement a BroadcastReceiver without extending Android BroadcastReceiver. This is not possible. The Android framework handles the dispatching and delivery of all broadcast Intents to all components that have registered themselves as being listeners for those Intents. When some application calls sendBroadcast(), Android checks if any BroadcastReceivers have registered as listener for that Intent and if it finds any, it instantiates the component (if it isn't already instantiated) and then calls the component's onReceive() method.
If you don't extend Android's BroadcastReceiver you cannot register your component as a listener and Android will not call your class' onReceive().
NOTE: Please explain what it is you really want to do if you want more help.
I'm not an expert in Android... but expert in everything else :P haha just kidding, i think that a BroadcastReceiver it's like a kind of (linux) crontab, it seems like Android already have some kind of piece of software (or internal service) running for this, and when you use it... you "program it" (to that "service") with selected filters and stuff, that's why is being called even if your app isn't running. OR MAYBE i'm TOTALLY WRONG :) but that's how i see it.
First of all:
import android.content.BroadcastReceiver;
Declare your Broadcast Receiver:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Stuff you want to do when it receives something
}
};
And finally create an intent filter with the Intents you want your Broadcast to read:
IntentFilter iF = new IntentFilter(); // Example with some music players
iF.addAction("com.android.music.metachanged");
iF.addAction("com.htc.music.metachanged");
iF.addAction("fm.last.android.metachanged");
iF.addAction("com.sec.android.app.music.metachanged");
iF.addAction("com.nullsoft.winamp.metachanged");
iF.addAction("com.amazon.mp3.metachanged");
iF.addAction("com.miui.player.metachanged");
iF.addAction("com.real.IMP.metachanged");
iF.addAction("com.sonyericsson.music.metachanged");
iF.addAction("com.rdio.android.metachanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
iF.addAction("com.andrew.apollo.metachanged");
iF.addAction("com.spotify.mobile.android.metadatachanged");
iF.addAction("com.spotify.music.metadatachanged");
registerReceiver(mReceiver, iF); // At the end you call your receiver with your intent Filter
Hope it helps
Is it possible to call the BOOT_COMPLETED broadcastreceiver programmatically? I use it in the normal way but I want to execute it another time during runtime.
You can't send ACTION_BOOT_COMPLETED yourself. According to the docs:
"This is a protected intent that can only be sent by the system."
http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED
You can of course send your own intent, and trigger the same code to be called.
You are welcome to call sendBroadcast() to trigger your own BroadcastReceiver.
Usually, it is simpler just to have the common code -- needed both at boot time and at other times -- in some static method or helper class. Then, you do not need to actually call sendBroadcast(), as you can just use the static method or helper class to do the work.
I am stuck up just now; issue is my one of activity has register one private broadcastReceiver and I need to unregister it and as usual I cannot change that file.
My approach is get all broadcast receiver information () for that intent by Package Manager.queryBraodcastReceiver method.
Now I want to get broadcast receiver object from it.
Is there any other approach to work out this problem?
Is there any other approach to work out this problem?
Fix the activity to unregister its receiver. You cannot do this from anywhere else in your app.
I want to stop the currently running MusicPlayer when the user unplugs the headphones (both wired & bluetooth).
I came across sevral posts where use of:
isWiredHeadsetOn(), isBluetoothA2dpOn()
is suggested.
But Android docs says isWiredHeadsetOn() is deprecated. What alternative should I use?
Thanks
From the docs:
isWiredHeadsetOn() - This method is deprecated. Use only to check is a headset is connected or not.
It sounds like it is still recommended for what you're doing, though the docs are worded poorly
I just read a post where someone was suggesting registering for the ACTION_HEADSET_PLUG broadcast event.
You can apparently get the state of it from: intent.getIntExtra("state", 0));
I use AudioManager.ACTION_AUDIO_BECOMING_NOISY event in my app.
Create your custom broadcast receiver:
private class HeadsetIntentReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context cntx, Intent intent)
{
String action = intent.getAction();
if(action.compareTo(AudioManager.ACTION_AUDIO_BECOMING_NOISY) == 0)
{
}
}
}; /* end HeadsetIntentReceiver */
Then register receiver:
headsetReceiver = new HeadsetIntentReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
mParentActivity.registerReceiver(headsetReceiver, mIntentFilter);
Don't forget to unregister it later.
According to the documentation for it, it's deprecated but suggested to be used ONLY for checking to see if a headset is connected or not. Should be fine to use.
I am trying to detect Screen backlight On / Off.
I've found android.intent.action.SCREEN_OFF related its event.
But I don't know how to use this.
Can you suggest me about how to detect screen backlight on / off ?
I missing some example or sample code.
Thanks in advance.
If you still need to use a BroadCastReceiver, you have to register it in code using the registerReceiver(receiver, filter) on one of your Activities.
The intents do not fire if you register them in the Manifest.
Create a BroadCastReciever, Add an Intent Filter including your Intent (android.intent.action.SCREEN_OFF).
Just Override the OnRecieve function in the BroadcastReciver with your code.
Be Sure to include the BroadcastReciever in the Manifest File!
good luck! :)
I think you can have a look at this class. The method isScreenOn() might be what you need.