Exemplo n.º 1
0
 @Override
 public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx)
     throws JSONException {
   XExtensionResult.Status status = XExtensionResult.Status.INVALID_ACTION;
   String result = "Unsupported Operation: " + action;
   if (COMMAND_START.equals(action)) {
     IntentFilter intentFilter = new IntentFilter();
     intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
     final XCallbackContext cbCtx = callbackCtx;
     if (null == mBroadcastReceiver) {
       mBroadcastReceiver =
           new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
               XLog.d(CLASS_NAME, "received broadcast of battery changing");
               updateBatteryInfo(intent, cbCtx);
             }
           };
       getContext().registerReceiver(mBroadcastReceiver, intentFilter);
     }
     XExtensionResult extensionResult = new XExtensionResult(XExtensionResult.Status.NO_RESULT);
     extensionResult.setKeepCallback(true);
     return extensionResult;
   } else if (COMMAND_STOP.equals(action)) {
     removeBatteryListener();
     sendUpdate(new JSONObject(), callbackCtx, false);
     return new XExtensionResult(XExtensionResult.Status.OK);
   }
   return new XExtensionResult(status, result);
 }
Exemplo n.º 2
0
 /**
  * 根据电池信息的改变新生成ExtensionResult,并且将该消息发送给js
  *
  * @param info 电池状态的json数据
  * @param keepCallback 是否保持回调
  * @param callbackCtx 回调上下文环境
  */
 private void sendUpdate(JSONObject info, XCallbackContext callbackCtx, boolean keepCallback) {
   XExtensionResult result = new XExtensionResult(XExtensionResult.Status.OK, info);
   result.setKeepCallback(keepCallback);
   callbackCtx.sendExtensionResult(result);
 }