开发者

POWER connected and disconnected listeners

开发者 https://www.devze.com 2023-03-15 15:15 出处:网络
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.

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("an开发者_运维百科droid.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消