Home

hogen

Follow Excellence. Success will chase you.

Home About Github Email

html通过native.js调用原生android API代码段总结

本篇文章是 native.js 系列的第三篇,都是以前做 Hybrid App 项目的重新总结,关于这部分DCloud 官网也有代码示例,地址在这儿,但是我发现有的代码段运行后是不能起作用的,这里就把我需要用到的几个模块重新修改了一下,如下:

调用Android SharePreference数据

function getDataFromAndroid(){
     var Toast = plus.android.importClass("android.widget.Toast");
     var Context = plus.android.importClass("android.content.Context");
     var SharedPreferences = plus.android.importClass("android.content.SharedPreferences");
     var sp = mainActivity.getSharedPreferences("data",Context.MODE_PRIVATE);
     var value1 = plus.android.invoke(sp,"getString","data1","");
     var value2 = plus.android.invoke(sp,"getString","data2","");
     var toast = Toast.makeText(mainActivity,"data1:"+value1+"\n"+"data2:"+value2,Toast.LENGTH_SHORT);
     toast.show();
}

调用Android本地分享

function shareContent(){
    var Intent = plus.android.importClass("android.content.Intent");
    var shareIntent = new Intent(Intent.ACTION_SEND);
    plus.android.invoke(shareIntent,"setType","text/plain");
    plus.android.invoke(shareIntent,"putExtra",Intent.EXTRA_TEXT,"this is my share text");
    <!--shareIntent.setType("text/plain");-->
    <!--shareIntent.putExtra(Intent.EXTRA_TEXT,"this is my share text");-->
    mainActivity.startActivity(Intent.createChooser(shareIntent,"share to..."));
}

直接拨打电话

function callPhone(){
  var Intent = plus.android.importClass("android.content.Intent");
  var Uri = plus.android.importClass("android.net.Uri");
  var uri = Uri.parse("tel:10086");
  var callIntent = new Intent("android.intent.action.CALL",uri);
  mainActivity.startActivity(callIntent);
}

获取手机内存信息

function getMemorySize() {
    var Toast = plus.android.importClass("android.widget.Toast");
    var Context = plus.android.importClass("android.content.Context");
    var ActivityManager = plus.android.importClass("android.app.ActivityManager");
    var memoryInfo = new ActivityManager.MemoryInfo();
    var activityService = mainActivity.getSystemService(Context.ACTIVITY_SERVICE);
    activityService.getMemoryInfo(memoryInfo);
    var availRAMSize = memoryInfo.plusGetAttribute("availMem");
    var toast = Toast.makeText(mainActivity,"可用内存:"+availRAMSize,Toast.LENGTH_SHORT);
    toast.show();
}

打开网络设置

/*var intent = new Intent('android.settings.WIFI_SETTINGS')*/
function openNetwork(){
    var Intent = plus.android.importClass("android.content.Intent");
    var Settings = plus.android.importClass("android.provider.Settings");
    <!--android.settings.SETTINGS和android.settings.WIRELESS_SETTINGS是同一个界面-->
    <!--var intent = new Intent('android.settings.SETTINGS');-->
    <!--用如下方式需要导入Settings包-->
    var intent = new Intent(Settings.ACTION_SETTINGS);
    mainActivity.startActivity(intent);
}

读取手机联系人

function readContacts(){
      var REQUESTCODE = 1000;
      var Intent = plus.android.importClass('android.content.Intent');
      var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
      var intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
      mainActivity.startActivityForResult(intent, REQUESTCODE);

      mainActivity.onActivityResult = function(requestCode, resultCode, data) {
          if (REQUESTCODE == requestCode) {
              var phoneNumber = null;
              var resultString = "";
              var context = mainActivity;
              plus.android.importClass(data);
              var contactData = data.getData();
              var resolver = context.getContentResolver();
              plus.android.importClass(resolver);
              var cursor = resolver.query(contactData, null, null, null, null);
              plus.android.importClass(cursor);
              cursor.moveToFirst();
              var s_ret;
              var givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
              s_ret = givenName;
              var contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
              var pCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
               null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
               null, null);
              while (pCursor.moveToNext()) {
               phoneNumber =   pCursor.getString( pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
               s_ret += '\n' + phoneNumber;
              }
              alert(s_ret);
              cursor.close();
              pCursor.close();
          }
      };
  }

上面都是一个个的函数,你只要在 html 中 button 点击事件调用如上方法即可


hogen

2017-05-06
Home About Github Email