microsoft / ace

Build Cordova apps with true native UI
http://microsoft.github.io/ace
Other
850 stars 157 forks source link

issue on Integrate with ionic 1 #47

Closed jenqtat closed 7 years ago

jenqtat commented 8 years ago

Hi, I added the Ace plugin to my Ionic 1 android project by using vs2015 plugin method and try to test Ace by following the sample of showing the battery level. However it hit the native error.

My code and error as below :-

controller :-

.controller('AccountCtrl', function($scope) {
$scope.battery = function() {
  if (ace.platform == "iOS") {
      // Objective-C code:
      // UIDevice* device = [UIDevice currentDevice];
      // [device setBatteryMonitoringEnabled:true];
      // double capacity = [device batteryLevel] * 100;
  // Invoke a static currentDevice method on UIDevice
      ace.NativeObject.invoke("UIDevice", "currentDevice", function (device) {
          // On the returned instance, call an instance method with no return value
          device.invoke("setBatteryMonitoringEnabled", true);
          // Call another instance method that returns a double
          device.invoke("batteryLevel", function (level) {
              alert("capacity = " + (level * 100) + "%");
          });
      });
  }
  else if (ace.platform == "Android") {
      // Java code:
      // BatteryManager batteryManager = getContext().getSystemService(Context.BATTERY_SERVICE);
      // int capacity = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

      // Get the value of the string constant Context.BATTERY_SERVICE
      ace.NativeObject.getField("android.content.Context", "BATTERY_SERVICE", function (constant) {
          // Invoke an instance method on the Android context object to get a BatteryManager instance
          ace.android.getContext().invoke("getSystemService", constant, function (batteryManager) {
              ace.NativeObject.getField("android.os.BatteryManager", "BATTERY_PROPERTY_CAPACITY", function (constant) {
                  batteryManager.invoke("getIntProperty", constant, function (value) {
                      alert("capacity = " + value + "%");
                  });
              });
          });
      });
      }
  }
  });

Error msg :-

Error in Error callbackId: NativeHost1968659433 : Error: Native error: java.lang.RuntimeException: Context does not have a BATTERY_SERVICE field at run.ace.Utils.getField(Utils.java:99) at run.ace.IncomingMessages.staticFieldGet(IncomingMessages.java:259) at run.ace.NativeHost$2.run(NativeHost.java:260) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5349) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) at dalvik.system.NativeStart.main(Native Method)

See http://ace.run/docs/errors for help. cordova.js (312,47) Uncaught Error: Native error: java.lang.RuntimeException: Context does not have a BATTERY_SERVICE field at run.ace.Utils.getField(Utils.java:99) at run.ace.IncomingMessages.staticFieldGet(IncomingMessages.java:259) at run.ace.NativeHost$2.run(NativeHost.java:260) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5349) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) at dalvik.system.NativeStart.main(Native Method)

See http://ace.run/docs/errors for help. ToNative.js (60,11)

Appreciate if you can advice and guide me on the issue above. Thanks

adnathan commented 8 years ago

The BATTERY_SERVICE field was added to Android's Context in API 21 (Lollipop) so my guess is that you're running on an older version of Android. If so, you'll just have to try a different sample. If not, let me know!