POWER connected and disconnected listeners - android

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.

Related

How to call a service method in a broadcastreceiver?

So, I have an app that starts a service. This service starts to scan for bluetooth devices with BTAdapter.startDiscovery(). Further I have a broadcastreceiver which listens for the DISCOVERY_FINISHED action. If that occurs I want to call a method from onReceive() in my service that starts the scanning process again. How am I gonna do this?
Here my receiver:
public class PollingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ScanBTService sBTs = new ScanBTService();
sBTs.startScan();
}
}
and here the service:
public class ScanBTService extends IntentService {
private BluetoothAdapter mBTAdapter;
private PollingReceiver mPollingReceiver;
public ScanBTService() {
super("ScanBTService");
}
#Override
protected void onHandleIntent(Intent intent) {
final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBTAdapter = btManager.getAdapter();
mBTAdapter.startDiscovery();
}
public void startScan() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mBTAdapter.startDiscovery();
}
}
In your onReceive()-method, restart your service using the following two lines. I did not tested it out but it should work like that.
#Override
public void onReceive(Context context, Intent intent) {
//ScanBTService sBTs = new ScanBTService();
//sBTs.startScan();
Intent i = new Intent(getApplicationContext(), ScanBTService.class);
startService(i);
}
You can then remove the startScan()-method, too.
Try this to resolve the method:
context.startService(new Intent(context, SimpleWakefulService.class));
Since you are using an IntentService, you will need to create an intent for the started service to handle.
This can be achieved by the following :
Intent intent = new Intent(context, ScanBTService.class);
startService(intent);
As described here : https://developer.android.com/training/run-background-service/send-request.html
Now, if you are looking to have a service that maintains bluetooth connections, discover devices, send & receive data... If this is the case, then in my experience, i would argue the following points :
Perhaps the best way to go about this (depending on what you're doing of course), would be to have a service running in it's own separate process which would be responsible for all of that. Check : http://developer.android.com/guide/topics/manifest/service-element.html and the tag
android:process
Take advantage of Android's IPC communication feature to pass & receive messages between you're main app thread and your service. Tutorial : http://www.survivingwithandroid.com/2014/01/android-bound-service-ipc-with-messenger.html.
Create & Maintain connection quick guide : http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient
Hope it helps
Cheers

Android How does PACKAGE_RESTARTED intent work?

I am new to Android development.
I want to get a notice when the operating system (because of lack of memory) or other application (Task killer e.g. ZDbox) restart other applicatoin (not mine).
I tried BroadcastReceiver. It did not get any Intent when an app was killed and the user started it manually. I have used PACKAGE_RESTARTED intent but BroadcastReceiver did not get any Intent.
Any suggestions?
public class MyService extends Service {
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
filter.addDataScheme("package");
BroadcastReceiver pkgRemoveReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do my stuff
}
};
registerReceiver(pkgRemoveReceiver, filter);
}

send action from receiver to activity?

I am using broadcast receiver in my app to detect incomming call and it works fine. But problem is I can not send action to activity. I mean.. I want do something in activity not in receiver. I read many tutorial but they all are performing action in receiver. Any idea ?
You can declare a BroadcastReceiver as inner class of the Activity. In this case you can directly call activity's methods:
public class MyActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
activityMethod();
}
};
private final IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
#Override
protected void onStart() {
super.onResume();
registerReceiver(receiver, filter);
}
#Override
protected void onStop() {
super.onPause();
unregisterReceiver(receiver);
}
private void activityMethod() {
}
}
You can start the Activity using an Intent and put a command code in the Intent extra fields. In your Activity you can then decide the behaviour based on the command code or resort to a default behaviour if none is present.
You can start an activity from your receiver via the normal means:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, YourActivity.class);
startActivity(i);
}
Note though that the user is going to expect that the phone application starts up since they are receiving a phone call. It is very likely a bad idea to hijack the phone call by dumping your own activity on top of the stock dialer app.

android is there Bluetooth receiver like sms?

hello everyone i face a real problem i have an arduino device that sends text information via bluetooth and i want to receive this text on my android phone but i want to check the ((text))before any reaction
is there any way to do that like SMS ((BroadcastReceiver))
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
I don't know how exactly to do that. But in the same way you use Receiver for SMS you can use Receiver for Bluetooth.
Register receiver (Manifest or in code)
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
And define the receiver
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name;
name = device.getName();
}
}
};
I suppose : with the communication you can catch a different pram in intent that gives to you the text.
if you want some examples take a look.
http://android-er.blogspot.com.es/2011/05/scan-bluetooth-devices.html AND
http://luugiathuy.com/2011/02/android-java-bluetooth/

Android broadcastReceiver issue power connection

I want to receive an interrupt when the android device starts charging either thr USB or AC
I have registered the receiver through the following code--
registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
The code which accepts the broadcast is--
public void onReceive(Context context, Intent intent) {
powerMonitor = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
{
msg = "power connected";
}
The issue is when I connect the device through a USB cable I fail to get the interrupt and if I try to remove and plug the USB cable,the VM gets disconnected..How do I check it?
Have I gone wrong?
I even tried d following code--
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(broadcastReceiver, filter);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged==BatteryManager.BATTERY_PLUGGED_AC ||plugged==BatteryManager.BATTERY_PLUGGED_USB)
{
msg="power connected"
}
Please help!!
please have a look on this link (The answer given by me)
activate an application when a power button is clicked
this will help you to solve your problem.

Categories

Resources