我已经按照Shai的原生接口教程进行了操作,但我仍然坚持使用codenameone中的android本机代码。一旦设备关闭,我需要获得电池电量(百分比)。我已经在原生android中完成了它,我在cn1中构建了应用程序,它也需要该功能。下面我提供了代码。请帮我在cn1中应用它。三江源
原生安卓代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for mobile shutdown starts
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
BroadcastReceiver mReceiver = new ShutDownReceiver();
registerReceiver(mReceiver, filter);
//for mobile shutdown ends
}
//for mobile shutdown
public class ShutDownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN))
{
Log.e("shutdown","mobile shutdown");
Log.e("shutdown battery percentage",getBatteryPCT() + "");
}
}
}
public float getBatteryPCT()
{
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
//Check if charging.
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
//Check if charger plugged in.
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
//check if charging via USB.
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
//check if charging via AC.
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
//Get the current battery percentage.
return batteryPct*100;
}
以下是我在cn1中尝试过的但是构建失败
public class MyNativeImpl {
public boolean isSupported() {
return true;
}
public void getShutDownBattery() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
BroadcastReceiver mReceiver = new ShutDownReceiver();
registerReceiver(mReceiver, filter);
getBatteryPCT();
Log.e("battery status", getBatteryPCT() + "");
}
public class ShutDownReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
Log.e("shutdown", "mobile shutdown");
Log.e("shutdown battery percentage", getBatteryPCT() + "");
}
}
}
public float getBatteryPCT() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float) scale;
return batteryPct * 100;
}
}
MyNative界面
public interface MyNative extends NativeInterface{
public void getShutDownBattery();
}
更新1:
您好Shai,关于您的答案,我有几个问题:
1)将ShutdownReceiver类移动到顶级类而不是内部类
为什么内心阶级不在这里工作?
2)需要从AndroidNativeUtil获取registerReceiver
的活动我没有找到任何关于你给出的链接的AndroidNativeUtil。以下行是否正确?
import com.codename1.impl.android.AndroidNativeUtil.registerReceiver(mReceiver,filter);
3)的权限
我需要什么权限?它在本机安卓代码中不需要任何权限。
更新的代码:
public class MyNativeImpl{
public boolean isSupported() {
return true;
}
public void getShutDownBattery() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
BroadcastReceiver mReceiver = new ShutDownReceiver();
AndroidNativeUtil.registerReceiver(mReceiver, filter);
}
}
class ShutDownReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
Log.e("shutdown", "mobile shutdown");
Log.e("shutdown battery percentage", getBatteryPCT() + "");
}
}
public float getBatteryPCT() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
//Check if charging.
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
//Check if charger plugged in.
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
//check if charging via USB.
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
//check if charging via AC.
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float) scale;
//Get the current battery percentage.
return batteryPct * 100;
}
}
答案 0 :(得分:0)
将ShutdownReceiver
类移动到顶级类而不是内部类。 registerReceiver
是一种活动方式,因此您需要AndroidNativeUtil
作为explained in the video获取活动。
您需要执行本机代码中的两件事,那些是权限。您需要向清单添加权限,您可以通过构建提示android.xpermissions
提示来执行此操作。
您还需要在代码中申请Android 6+权限,并且可以通过AndroidNativeUtil
调用checkForPermission
按照here的说明进行操作。