打包app获取权限(位置/通讯录/通话记录/短信)

在manifest.json勾选对应权限,重新打包生效(包括选择自定义基座调试)
1、获取位置详细地址:https://blog.csdn.net/weixin_44052462/article/details/105135519?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

2、获取通讯录(可用):https://ask.dcloud.net.cn/article/35637

3、通话记录(仅供参考,不能用):https://blog.csdn.net/Ericu/article/details/102782614

4、获取短信内容(可用,这里只使用短信代码):https://blog.csdn.net/WoSNBBCSDN/article/details/107082537?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-4-107082537.nonecase&utm_term=uniapp%E8%AF%BB%E5%8F%96%E9%80%9A%E8%AE%AF%E5%BD%95

<template>
	<view class="content">
		<view class="contact">
			<button type="default" @click="getContact">通讯录</button>
		</view>
		<view class="lisBox">
			<block v-for="(item,index) in phoneList" :key="index">
				<view>{{item.displayName}}</view>
				<block v-for="(subitem,idx) in item.phoneNumbers" :key="idx">
					<view>{{subitem.value}}</view>
				</block>
			</block>
		</view>
		<view class="contact">
			<button type="default" @click="requestCallPermission">通话记录</button>
		</view>
		<view class="lisBox">
			<view class="contact-list" v-for="(item, index) in getPhoneLogLis" :key="index"
				style="border-bottom: 1px solid #000000; margin: 8px;">
				<view>
					<view>手机号:{{item.mobile}}</view>
					<view>类型:{{item.type}} {{item.callTime}}</view>
					<view>通话时长:{{item.talkTime}}</view>
				</view>
			</view>
		</view>
		<view class="message">
			<button class="get-message" @click="getMessage">获取短信</button>
		</view>
		<view class="lisBox">
			<view class="contact-list" v-for="(item, index) of contactList" :key="index"
				style="border-bottom: 1px solid #000000; margin: 8px;">
				<view>
					<view>短信号码:{{item.address}}</view>
					<view>内容:{{item.body}}</view>
				</view>
			</view>
		</view>
		<view>系统信息</view>
		<view class="lisBox" style="border-bottom: 1px solid #000000; margin: 8px;">{{stemInfo}}</view>
		<view @click="getposition">位置信息</view>
		<view class="lisBox" style="border-bottom: 1px solid #000000; margin: 8px;">{{positionLis}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneList: [],
				contactList: [],
				getPhoneLogLis: [],
				stemInfo: '',
				positionLis: ""
			};
		},

		onShow() {
			var that = this
			this.getContact();
			this.requestCallPermission()
			this.getMessage();

			// 获取系统信息
			uni.getSystemInfo({
				success: function(res) {
					that.stemInfo = '品牌' + res.brand + '型号' + res.model + '系统版本' + res.system
					console.log('系统信息', that.stemInfo)
				}
			});
		},

		onLoad() {
			this.getposition();
		},

		methods: {
			// 通讯录
			getContact() {
				var that = this
				// 获取通讯录对象
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					// uni.showToast({
					// title: '获取通讯录对象成功',
					// duration: 2000
					// })
					// console.log(addressbook)

					// 查找联系人
					addressbook.find(["displayName", "phoneNumbers"], function(contacts) {
						// uni.showToast({
						// title: '获取联系人成功',
						// duration: 2000
						// })

						that.phoneList = contacts
						console.log('通讯录', that.phoneList)
					}, function() {
						// uni.showToast({
						// title: '获取联系人失败',
						// duration: 2000
						// })
					}, {
						multiple: true
					});
				}, function(e) {
					// uni.showToast({
					// title: '获取通讯录对象失败:' + e.message,
					// duration: 2000
					// })
				});
			},

			// 短信
			getMessage() {
				var that = this
				//这分代码来自于https://www.cnblogs.com/jucheng/p/12176346.html
				//不是原创哈~~~~~
				// console.log('getmessage');
				//不同机型可能获取不全
				//查看权限是否已允许
				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');
				// res == -1 时为询问状态,询问时会走Show 和 Hidden
				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 msgcontactList = []
					while (cur.moveToNext()) {
						var msgLis = {}
						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);
						console.log(address, body, type);
						msgLis.address = address
						msgLis.body = body
						msgcontactList.push(msgLis)
						// 收件人 address
						// 短信内容 body
						// 短信的类型 type
					}
					that.contactList = msgcontactList
					console.log('短信集合', that.contactList)
					cur.close();
				}
			},

			// 通话记录
			oclaik() {
				var that = this
				var CallLog = plus.android.importClass("android.provider.CallLog");
				var Activity = plus.android.runtimeMainActivity();
				var ContentResolver = plus.android.importClass("android.content.ContentResolver");
				var resolver = Activity.getContentResolver();
				plus.android.importClass(resolver);
				var String = plus.android.importClass("java.lang.String");
				var cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
				plus.android.importClass(cursor)
				var content = []; // 用来存储数据
				if (cursor.moveToFirst() == true) {
					while (cursor.moveToNext()) {
						//号码
						var number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
						//呼叫类型
						var type;
						switch (
							parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE))))
						// 判断通话类型
						{
							case CallLog.Calls.INCOMING_TYPE:
								type = "呼入";
								break;
							case CallLog.Calls.OUTGOING_TYPE:
								type = "呼出";
								break;
							case CallLog.Calls.MISSED_TYPE:
								type = "未接";
								break;
							default:
								type = "挂断"; //应该是挂断.根据我手机类型判断出的
								break;
						}

						// 获取时间
						var date = new Date(parseInt(
							cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE))));
						// 时间格式转换
						var times = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date
							.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
						// 联系人
						var Name_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME);
						var name = cursor.getString(Name_Col);
						// 号码归属地 返回:北京 联通
						var numberLocation = cursor.getString(
							cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)
						);
						//通话时间,单位:s
						var Duration_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION);
						var duration = cursor.getString(Duration_Col);
						// 存入数组
						content.push({
							name: name, // 联系人的姓名
							mobile: number, // 联系人电话
							numberLocation: numberLocation, // 号码的归属地
							callTime: times, // 呼入或呼出时间
							talkTime: duration, // 通话时长
							type: type
						});
					}
				}
				// uni.showToast({
				// title: "通话记录:" + JSON.stringify(content),
				// duration: 5000
				// })
				that.getPhoneLogLis = content
				console.log('通话记录', that.getPhoneLogLis)
			},
			requestCallPermission() {
				var that = this
				plus.android.requestPermissions(['android.permission.READ_CALL_LOG'], function(e) {
					if (e.deniedAlways.length > 0) { //权限被永久拒绝
						// 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启
						console.log('Always Denied!!! ' + e.deniedAlways.toString());
					}
					if (e.deniedPresent.length > 0) { //权限被临时拒绝
						// 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限
						console.log('Present Denied!!! ' + e.deniedPresent.toString());
					}
					if (e.granted.length > 0) {
						// uni.showToast({
						// title: "已授权",
						// duration: 5000
						// })
						that.oclaik()
					}
				})
			},

			//获取定位
			getposition() {
				var that = this
				uni.getLocation({
					type: 'wgs84',
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: function(res) {
						that.positionLis = res
						console.log('位置信息', res)
					},
					fail: function() {
						uni.showToast({
							title: '获取地址失败,将导致部分功能不可用',
							icon: 'none'
						});
					}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.lisBox {
		height: 100px;
		width: 100%;
		background-color: pink;
		margin: 0 auto;
		overflow: hidden;
		overflow-y: auto;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

你可能感兴趣的:(#,uni-app,javascript,前端,vue.js)