I have the following broadcast receiver:
public class MyRingModeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Logger.iLog("In my Receiver");
}
}
I then have a service that onCreate does the following:
IntentFilter filter = new IntentFilter();
filter.addAction("android.media.RINGER_MODE_CHANGED");
registerReceiver(new MyRingModeReceiver() , filter);
When I place a call to the emulator and use the volume keys to modify (silence) the ringer
nothing happens. Any ideas of what it is that i am doing wrong?
Thanks.
Apparently, the Intent Ringer_MODE_CHANGED is not called when you silence a ringer using the phone app ... Only when you modify the ringer outside of a phone call.
Related
I make these codes in my service:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.MEDIA_BUTTON");
intentFilter.setPriority(2147483647);
registerReceiver(broadcastReceiver,intentFilter);
And that's the Receiver
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
playNext(); // play the next music
}
};
But I never receiver the broadcast, I don't know why.
And every time I tap on the headset, the below will show
I/MediaButtonIntentReceiver: handle action:android.intent.action.MEDIA_BUTTON
I/MediaButtonIntentReceiver: action = 1, repeatCount = 0, command = togglepause, keycode = KEYCODE_HEADSETHOOK
Rocco is right.
You may need to obtain permission:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
if you have more than one reciever in your class:
you register "broadcastReceiver", but named it "broadcastReceiver1*"
I am creating an SMS app. I can send messages fine, however I cannot get it to receive. I have successfully implemented the functionality to allow the app to be selected as the default SMS application on the device.
The problem I have is that I cannot pass the SMS from the BroadcastReceiver to the Activity that displays messages. I am aware of the ability to use intent.putExtra() for the message and then startActivity(), but what happens if that activity has already been started when the message is received? I do not want to restart the activity every time a new message is received.
There are few ways to skin that cat, one way is to have a receiver inside the Activity something like this
void onResume(){
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSmsReceiver, filter);
}
void onPause(){
super.onPause();
unregisterReceiver(mSmsReceiver);
}
private BroadcastReceiver mSmsReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Do you stufff
}
};
hi is there is any code that stop play sound when i lock the mobile .As I had created Android app which play sound in specific time and i want user to mute the sound when press at lock button .
You need to listen for screen off intent
android.intent.action.ACTION_SCREEN_OFF
Ok. Look into documentation http://developer.android.com/guide/components/intents-filters.html for more detailed info about intent handling. Here is one of methods:
#Override
protected void onResume() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new IntentListener(), intentFilter);
super.onResume();
}
And inner class defining IntentListener
private class IntentListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//here code do be executed on when intent comes
}
}
Also remember to unregister receiver in onPause():
unregisterReceiver(intentListener);
I haven't checked this code, so please do it yourself
I've a Broadcast receiver:
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Do something
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent start=new Intent(context,MainActivity.class);
context.startActivity(start);
}
}
}
And, in my activity, into onCreate():
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver=new ScreenReceiver();
registerReceiver(mReceiver, filter);
The problem is that, when my activity is displayed, the receiver performs correctly the action, but when it is in background, sometimes nothing happens.
What could be the issue?
Most likely, when your app goes into the background Android kills it to free up resources. Try starting a foreground service attached to an ongoing notification from your Activity, and register the BroadcastReceiver in that.
I'm trying to turn the GPS location updates off when the screen locks. Having read the answer to this question Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I've written some code to implement a BroadcastReceiver but it's not working when the screen goes off.
I've registered a BroadcastReceiver in my code with
#Override
public void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("android.intent.action.ACTION_SCREEN_ON");
iFilter.addAction("android.intent.action.ACTION_SCREEN_OFF");
registerReceiver(screenStatReceiver, iFilter);
and the receiver itself is just a stub for now:
public BroadcastReceiver screenStatReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("BCAST_TAG", "Got broadcast");
String action = intent.getAction();
}
};
and in the manifest I have:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Any ideas as to why it's not being triggered when I debug it on my phone?
For this problem, you have to create a infinite service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off but make sure you have to make service which will always running in background like reboot receiver
You are registering the reciever in the onResume() method. Why dont you register it in the onCreate() so you can have persistent "listening"?
In this case you are listening for changes only after the activity is resumed.