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*"
Related
I am trying to use BroadcastReceiver inside my service but it is not working properly.
I am starting my service in an onCreate in my activity. Then in the services onCreate I am calling the following to register the Broadcast reciever:
IntentFilter filter = new IntentFilter();
registerReceiver(DataUpdateReceiver, filter);
Here's the broadcast receiver i am trying to register:
private BroadcastReceiver DataUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
};
Then else where in the Activity I am trying to call this so therefor the Toast message will be displayed.
Intent i = new Intent();
sendBroadcast(i);
But the Toast is not being displayed, I have also tried logging but nothing shows up. If anyone could help me out on this it would be appreciated, ty.
In my opinion, you have to specify action (or actions), which fire onReceive() method. Something like this might help you:
IntentFilter filter = new IntentFilter("some_action");
registerReceiver(DataUpdateReceiver, filter);
...
Intent i = new Intent("some_action");
sendBroadcast(i);
Declare on top of the class
public final static String MY_RECEIVER_START = "com.yourcompanyname.appname.MY_RECEIVER_START";
private Radio radio;
In the service constructor
//Initiate our receiver
radio = new Radio();
//Activate our recevier
context.registerReceiver(radio, new IntentFilter(MY_RECEIVER_START));
Also in the service, create the receiver class and the method which shows toast
/**
* Receiver Class
* This setup checks for the incoming intent action to be able to
* attach more messages to one receiver.
*/
private class Radio extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MY_RECEIVER_START)){
//show toast
}
}
}
After from anywhere in the application send message to our radio
context.sendBroadcast(new Intent("com.yourcompanyname.appname.MY_RECEIVER_START"));
I'm stuck here at my previous struggle >> Prev. Struggle!
Raanan there helped! me a lot but then he I think went away as timing zone is different , now I'm stuck with my service code that I'm using to call my BroadcastReceiver() that is in the activity! and also I'm not getting with what parameter I should load the filter.addAction(action); in place of action??
Kinldy guide me!
CODE in the Server:
Toast.makeText(Server.this, hr +" , " +min, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, andRHOME.class);
//intent.putExtra("sendMessage","1");
sendBroadcast(intent);
and CODE IN THE ACITIVITY(Broadcast Receiver)
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN DA BroadCASTER",
Toast.LENGTH_LONG).show();
sendMessage("1");
}
};
IntentFilter filter = new IntentFilter();
You need to add these line to regiester your receiver for some action for example define a Global variable like this:
public static String NOTIFCATION_BROADCAST_ACTION = "com.your_packagename.UPDATE_NOTIFICATION_INTENT";
then register the action like this in your activity onCreate() Method.
IntentFilter filter = new IntentFilter();
filter.addAction(Global.NOTIFCATION_BROADCAST_ACTION);
registerReceiver(ReceivefrmSERVICE, filter);
Then send the broadcast from your service like this
Intent broadcast = new Intent();
broadcast.setAction(Global.NOTIFCATION_BROADCAST_ACTION);
sendBroadcast(broadcast);
Then in your broadcast Receiver filter this action like this
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Global.NOTIFCATION_BROADCAST_ACTION)) {
//Do your stuff here :)
}
}
};
As described in question I want to start a broadcast receiver on some event say a button click so I don't want to use it in xml. Any idea how to do this I searched on net but most of example are using xml for this
Regard's
Saurabh
Put this code to your button onClick listener. It creates a receiver, handler, and intent filter, sets the action your receiver should be registered for and register it. Dont' forget to unregister it after all the work will be done.
// this goes before onCreate()
private static final String ACTION = "YOUR_ACTION_HERE";
/// in button listener:
Handler mHandler = new Handler();
BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Handle reciever
String mAction = intent.getAction();
if(mAction.equals(ACTION) {
// Do your thing
}
}
IntentFilter intentToReceiveFilter = new IntentFilter();
intentToReceiveFilter.addAction(ACTION);
this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler);
How to know Android Phone is going to sleep?
Please Help me with a sample code.
Thanks for reading.
You'll need to register a broadcastreceiver for Intent.ACTION_SCREEN_OFF
So, create the broadcastreceiver
This is where you handle the screen_off intent.
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(final Context context, final Intent intent) {
/*
* dispatch screen_off
* to handler method
*/
String iAction = intent.getAction();
if (iAction.equals(Intent.ACTION_SCREEN_OFF))
handleScreenAction(iAction);
}
};
Now the filter to register the receiver.
static void registerReciever() {
IntentFilter myFilter = new IntentFilter();
// Catch screen off event
myFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, myFilter);
}
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.