本篇文章是 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 點選事件呼叫如上方法即可