2017-08-05 62 views
1

我需要在設備關閉後立即獲取電池電量(百分比)。以下代碼給出了構建失敗的錯誤。我在下面包含了代碼和構建錯誤。謝謝。codenameone中用於獲取電池信息的本地接口(重複)

MyNativeImpl.java

public class MyNativeImpl { 

    public boolean isSupported() { 
     return true; 
    } 

    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; 
    } 

    public void getShutDownBattery() { 
     if (!AndroidNativeUtil.checkForPermission(Manifest.permission.DEVICE_POWER, "This should be the description shown to the user...")) { 
      Log.e("no permission"); 
     } 
     IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN); 
     BroadcastReceiver mReceiver = new ShutDownReceiver(); 
     AndroidNativeUtil.getActivity().registerReceiver(mReceiver, filter); 

     getBatteryPCT(); 
     Log.e("battery status", getBatteryPCT() + ""); 
    } 
} 

class ShutDownReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context, Intent intent) 
    { 
     if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) 
     { 
      Log.e("shutdown","mobile shutdown"); 

      MyNativeImpl ma = ((MyNativeImpl)context); 
      Log.e("shutdown battery percentage",ma.getBatteryPCT() + ""); 
     } 
    } 

} 

爲MyNative接口

public interface MyNative extends NativeInterface{ 
    public void getShutDownBattery(); 
} 

生成錯誤

All input files are considered out-of-date for incremental task ':compileDebugJavaWithJavac'. 
Compiling with source level 1.7 and target level 1.7. 
:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.). 
file or directory '/tmp/build745725138395090105xxx/MyApplication/src/debug/java', not found 
Compiling with JDK Java compiler API. 
/tmp/build745725138395090105xxx/MyApplication/src/main/java/com/mycompany/myapppp/MyNativeImpl.java:21: error: cannot find symbol 
     Intent batteryStatus = this.registerReceiver(null, ifilter); 
           ^
    symbol: method registerReceiver(<null>,IntentFilter) 
/tmp/build745725138395090105xxx/MyApplication/src/main/java/com/mycompany/myapppp/MyNativeImpl.java:53: error: incompatible types: Context cannot be converted to MyNativeImpl 
      MyNativeImpl ma = ((MyNativeImpl)context); 
              ^
Note: Some input files use or override a deprecated API. 
Note: Recompile with -Xlint:deprecation for details. 
Note: Some input files use unchecked or unsafe operations. 
Note: Recompile with -Xlint:unchecked for details.  
2 errors 
:compileDebugJavaWithJavac FAILED 
:compileDebugJavaWithJavac (Thread[Daemon worker,5,main]) completed. Took 10.298 secs. 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':compileDebugJavaWithJavac'. 
> Compilation failed; see the compiler error output for details. 

* Try: 
Run with --debug option to get more log output. 

* Exception is: 
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileDebugJavaWithJavac'. 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69) 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46) 
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35) 
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64) 
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52) 
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52) 
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53) 
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) 
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203) 
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185) 
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66) 
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50) 
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25) 
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110) 
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37) 
    at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExecuter.java:23) 
    at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecuter.java:43) 
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37) 
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30) 
    at org.gradle.initialization.DefaultGradleLauncher$4.run(DefaultGradleLauncher.java:154) 
    at org.gradle.internal.Factories$1.create(Factories.java:22) 
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90) 
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52) 
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151) 
    at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32) 
    at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:99) 
    at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:93) 
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90) 
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62) 
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93) 
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82) 
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94) 
    at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28) 
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) 
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43) 
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28) 
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:75) 
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:45) 
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:52) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72) 
    at org.gradle.util.Swapper.swap(Swapper.java:38) 
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:40) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:72) 
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41) 
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50) 
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:246) 
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54) 
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40) 
Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details. 
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:47) 
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:33) 
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:103) 
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:52) 
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:38) 
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:34) 
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25) 
    at org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:38) 
    at org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:24) 
    at org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:157) 
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:127) 
    at com.android.build.gradle.tasks.factory.AndroidJavaCompile.compile(AndroidJavaCompile.java:49) 
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75) 
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:244) 
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220) 
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:231) 
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209) 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80) 
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61) 
    ... 68 more 


BUILD FAILED 

Total time: 22.029 secs 
Stopped 0 compiler daemon(s). 
Received result 

失敗[值= org.gradle.initialization.ReportedException:org.gradle.internal。 exceptions.LocationAwareException:執行任務':compileDebugJavaWithJavac'。]中的daemo失敗n DaemonInfo {pid = 12903,address = [d9c0125e-4515-4cce-a621-88bec0268233 port:39233,address:[/ 0:0:0:0:0:0:0:1%lo,/127.0.0.1] ],idle = false,context = DefaultDaemonContext [uid = 63ed418a-b2be-41ad-ae20-1c2e777c822a,javaHome = /home/ec2-user/jdk1.8.0_45,daemonRegistryDir =/home/ec2-user/.gradle/daemon, PID = 12903,的idleTimeout = 120000,daemonOpts = -XX:MaxPermSize參數= 512M,-XX:+ HeapDumpOnOutOfMemoryError,-Xmx2048m,-Dfile.encoding = UTF-8,-Duser.country = US,-Duser.language = EN - Duser.variant]}(編譯應該做的事).`

如果你想看看整個生成錯誤,here it is.

+0

實際的構建錯誤不在這裏。他們在'3錯誤'行之上,這是你列出的第一行 –

+0

Ohhh謝謝你..我現在能夠將錯誤降到2。我在上面添加了這些錯誤。請看一看。如果需要,我在問題結尾提供了整個構建錯誤的鏈接。 – beck

+0

我已經通過添加AndroidNativeUtil.getActivity()。registerReceiver解決了registerReceiver錯誤。我還有一個構建錯誤需要解決。即。無法將上下文轉換爲MyNativeImpl – beck

回答

0

context是活動不是你實際上並不需要本機接口實例。您可以通過在常規類中調用一個靜態方法來回調Codename One代碼。

+0

我沒有找到您。一個例子很有幫助。不過,我使用ShutDownReceiver類作爲MyNativeImpl中的內部類,它工作。謝謝。還有一件事,雖然我沒有要求DEVICE_POWER權限(在上面的問題中評論了權限代碼),但它仍然可以在棉花糖中使用。 – beck