【实战】UniApp获取通讯录、短信、定位、手机型号等

警告:此方法仅供项目学习,请勿非法用途!

 

获取短信:

//不同机型可能获取不全
//查看权限是否已允许
var Context = plus.android.runtimeMainActivity();
var res = plus.android.invoke("android.support.v4.app.ActivityCompat", "checkSelfPermission", Context,	"android.permission.READ_SMS");
var PERMISSIONS_STORAGE = new Array();
PERMISSIONS_STORAGE.push("android.permission.READ_SMS"); 
if (res != "0") 
{	
	 plus.android.invoke("android.support.v4.app.ActivityCompat", "requestPermissions", Context, PERMISSIONS_STORAGE, 1);
} else {	
			    
}
var main = plus.android.runtimeMainActivity();
var Uri = plus.android.importClass("android.net.Uri");
var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
var uri = Uri.parse("content://sms/");
var cr = main.getContentResolver();
plus.android.importClass(cr);
var cur = cr.query(uri, null, null, null, null);
plus.android.importClass(cur);
cur.moveToFirst();
var smslist = [];
console.log(cur);
while (cur.moveToNext()) 
{
	var index_Address = cur.getColumnIndex("address");
	var address = cur.getString(index_Address);
	//短信内容
	var index_Body = cur.getColumnIndex("body");
	var body = cur.getString(index_Body);
	//类型1接收 2发送
	var index_Type = cur.getColumnIndex("type");
	var type = cur.getString(index_Type);
	smslist.push({
		address:address,
		body:body,
		type:type
	})
}
console.log(JSON.stringify(smslist));

 

获取定位信息:

uni.getLocation({
	type: 'wgs84',
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
	},
	complete:function(res){

	},
	fail:function(res3){
		
	}
});

 

获取通讯录:

plus.contacts.getAddressBook( plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
		// 查找联系人
		addressbook.find(["displayName","phoneNumbers"],function(contacts){

			var list = [];
			for(var i =0;i<contacts.length;i++){
				if(contacts[i].phoneNumbers.length<=0){
					continue;
				}
				// var phoneNumbers = [];
				// for(var a = 0 ; a<contacts[i].phoneNumbers.length;a++){
					// phoneNumbers.push({
						contacts[i].phoneNumbers[0].value;
				// 	})
				// }
				list.push({
					displayName:contacts[i].displayName,
					phoneNumbers:contacts[i].phoneNumbers[0].value
				});
				console.log(list[i]);
			}
		}, function () {
			// uni.showToast({
			//     title: '获取联系人失败',
			//     duration: 2000
			// })
		},{multiple:true});
	}, function ( e ) {
		// uni.showToast({
		//     title: '获取通讯录对象失败:' + e.message,
		//     duration: 2000
		// })

	});

}

 

获取手机型号:

           uni.getSystemInfo({
				success: function (res) {
					that.$data.platform = res.platform;
					that.$data.pinpai = res.model;
					console.log(res);
				}
			});