【发布时间】:2015-09-16 12:23:02
【问题描述】:
我正在尝试进行两次祝酒:一次在设备充电时,一次在未充电时。 但是接收者表现得很疯狂,发送了很多祝酒词,并使应用程序崩溃。 我找不到问题。 谢谢! 这是主要活动中的接收者:
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
if (isCharging){
Toast.makeText(context, "The device is charging", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "The device is not charging", Toast.LENGTH_SHORT).show();
}
}
}
这是清单:
<receiver android:name=".view.MainActivity$PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
【问题讨论】:
-
请分享 Log cat traces,以便我们可以确定 android 应用程序崩溃的确切问题
-
无法实例化接收器 com.gidi.places.easymap.view.MainActivity$PowerConnectionReceiver: java.lang.InstantiationException: class com.gidi.places.easymap.view.MainActivity$PowerConnectionReceiver 没有零参数构造函数
-
在 onResume 中注册接收器,在 OnPause 中取消注册接收器