Battery changed receiver not working - android

I want to react to the charging state in my app.
I registered the receiver for it in onCreate()
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
the receiver looks like that:
private BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
String test = Integer.valueOf(plugged).toString();
Toast.makeText(getApplicationContext(), test,
Toast.LENGTH_LONG).show();
}
};
but even if the device is plugged in the plugged variable is 0. Any idea how to fix that?

You could try to use BatteryManager.EXTRA_STATUS. The BatteryManager.EXTRA_PLUGGED can be used if you want more details about the type of power source I believe.
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent intentResult = registerReceiver(null, filter);
int state = intentResult.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
switch (state) {
case BatteryManager.BATTERY_STATUS_CHARGING:
case BatteryManager.BATTERY_STATUS_FULL:
//charging
break;
default:
//not charging
}

Related

screen off does not Recive Broadcast

Screen Off ,Broadcast Receiver does not call some time it will execute but mostly wifi state change event is called .i have also set up the priority of screen off but does not call or some time call .can you please tell .when my screen off i want to execute first then other wifi state changed will called
BroadcastReceiver wReceiver = new ScreenReciver();
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.setPriority(1);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.setPriority(1000);
registerReceiver(wReceiver, filter);
}
#Override
protected void onPause() {
unregisterReceiver(wReceiver);
super.onPause();
}
public class ScreenReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
Intent myintent = new Intent(context, TimerClockActivity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
wifiStateText = "WIFI_STATE_DISABLED";
break;
default:
break;
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent myintent = new Intent(context, TimerClockActivity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
}
}
}
You are calling unregisterReceiver(wReceiver); in onPause(). This means that every time that the Activity goes into the background (including when the screen turns off), your Activity is unregistered for that broadcast.
onPause() is likely getting called before your Activity gets a chance to receive the Broadcast.
Perhaps you want to place unregisterReceiver(wReceiver); in onDestroy() instead?

How can I detect usb connection in Android 4.2?

I used ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED to detect USB connection on Nexus 4, but I cannot receive any broadcast signal.
Here is my broadcast receiver code:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
debugReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
debugOn = true;
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
debugOn = false;
}
}
};
registerReceiver(debugReceiver, filter);
Any ideas? I also searched others questions; they said if I add
"filter.addDataScheme("file");"
I will get the signal, but I have tried and nothing was received.
Use UsbManager.ACTION_USB_DEVICE_ATTACHED and UsbManager.ACTION_USB_DEVICE_DETACHED for your intent filter.
To check for connection to PC use Intent.ACTION_BATTERY_CHANGED and onReceive intent.getInt(BatteryManager.EXTRA_PLUGGED) and see if the value is BatteryManager.BATTERY_PLUGGED_USB.

addAction() & Service calling Dilemma

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 :)
}
}
};

Bluetooth Server and BroadCastReceiver

My application holds an open bluetooth server socket with a specific UUID, in order for another device to connect and transfer files. I'm a bit confused regarding the BroadcastReceiver.
In my class which extends Activity, I want to check the state of the bluetooth adapter. But my BroadcastReceiver is never triggered. I tried using the BroadcastReceiver this way:
public class MainClass extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.w("BroadcastReceiver: ", "Inside!");
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Log.d("Bluetooth Receiver", "State-off");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d("Bluetooth Receiver", "State turning off");
break;
case BluetoothAdapter.STATE_ON:
Log.d("Bluetooth Receiver", "State-on");
btCom = new BluetoothCommunicator(MainClass.this, lastCases, nist);
btCom.startServer();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d("Bluetooth Receiver", "State turning on");
break;
}
}
}
};
}
I have a question regarding the states:
The state STATE_ON is this only fired off when the bluetooth is turned on during runtime? Or can I start my application with bluetooth turned on, and this event will be fired off? Cause I want to start the method btCom.startServer() if bluetooth is turned on
I also read that I need to register the broadcast receiver in my Manifest file, how can I do so if the BroadcastReceiver is in a class which extends Activity? If I had this BroadcastReceiver in a separate class I would do it like this
Say for instace that my Package Name was com.workbench and my Class name was BluetoothReceiver
The Manifest would look something like this:
<receiver android:name="com.workbench.BluetoothReceiver"></receiver>
The broadcast action BluetoothAdapter.ACTION_STATE_CHANGED is sent when the state of the bluetooth adapter changes. You will only see this when the state of the adapter is changed.
You can check the current state of the bluetooth adapter by calling BluetoothAdapter.isEnabled().
You only need to register the BroadcastReceiver in your manifest if you want to get the broadcast Intent when your application is not running. The way you have implemented the BroadcastReceiver (as an anonymous class) it isn't possible to register it in the manifest.

POWER connected and disconnected listeners

I am listening to both connecting and disonnecting the power for my galaxy.
I have creatd 2 BroadCastReceivers, one for connect, and one for disconnect.
When I try to implement, I only get the connected data, even when disconnecting the power cable.
The intent is sent, but looks like it's the wrong one.
Here is the activity code:
// Handle Power On
PowerConnectedBCReceiver myPowerConnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerOnFilter = new IntentFilter();
intentPowerOnFilter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
registerReceiver(myPowerConnectedBCReceiver, intentPowerOnFilter);
BroadcastReceiver PowerConnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("connected");
}
};
// Handle Power Off
PowerConnectedBCReceiver myPowerDisonnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerDisconnectedOnFilter = new IntentFilter();
intentPowerDisconnectedOnFilter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
registerReceiver(myPowerDisonnectedBCReceiver, intentPowerDisconnectedOnFilter);
BroadcastReceiver PowerDisconnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("disconnected");
}
};
Registering both BC to do the work
registerReceiver(PowerDisconnectedReceiver, new IntentFilter("com.neglected.POWER_DISCONNECTED"));
registerReceiver(PowerConnectedReceiver, new IntentFilter("com.neglected.POWER_CONNECTED"));
BroadCast connected code:
public class PowerConnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was connected to power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_CONNECTED");
context.sendBroadcast(tIntent);
}
}
Broadcast disconnected code:
public class PowerDisconnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was disconnected from power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_DISCONNECTED");
context.sendBroadcast(tIntent);
}
}
IS the code wrong?
Can I listen to both actions? seperately?
I can't see extra been sent with the CONNNECTED Action, is there?
Not sure what your last two classes (*BCReeivers) are supposed to be doing. Your first block of code looks ok. It will be limited to the lifecycle of the enclosing Activity if that matters.
For the Galaxy S, you may not be able to rely on those Intents. In particular, I have found that the Verizon Fascinate (their version of the Galaxy S) to be very buggy. See here: http://devblog.bu.mp/how-to-ddos-yourself
There was an error in my Broadcast instantiation. I mistakenly used the PowerConnectedBCReceiver instead of PowerDisconnectedBCReceiver
problem solved.

Categories

Resources