我正在倾听连接和断开我银河系的力量。
我创建了2个BroadCastReceivers,一个用于连接,另一个用于断开连接。
当我尝试实施时,即使断开电源线,我也只能获得连接的数据。
意图被发送,但看起来是错误的。
以下是活动代码:
// 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");
}
};
注册BC以完成工作
registerReceiver(PowerDisconnectedReceiver, new IntentFilter("com.neglected.POWER_DISCONNECTED"));
registerReceiver(PowerConnectedReceiver, new IntentFilter("com.neglected.POWER_CONNECTED"));
BroadCast连接代码:
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);
}
}
广播断开代码:
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);
}
}
代码错了吗? 我可以听这两个动作吗? seperately? 我看不到额外发送了CONNNECTED动作,是吗?
答案 0 :(得分:1)
不确定你的最后两个班级(* BCReeivers)应该做什么。你的第一段代码看起来不错。如果重要的话,它将限于封闭活动的生命周期。
对于Galaxy S,您可能无法依赖这些意图。特别是,我发现Verizon Fascinate(他们的Galaxy S版本)非常大。见这里:http://devblog.bu.mp/how-to-ddos-yourself
答案 1 :(得分:1)
我的广播实例中出错。我错误地使用 PowerConnectedBCReceiver 而不是 PowerDisconnectedBCReceiver
问题解决了。