我正在尝试编写代码以从 Android 应用程序发送短信,但是当我尝试发送短信时,它向我发回错误:
09-17 18:37:29.974 12847-12847/**.**.****E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: **.**.****, PID: 12847
java.lang.SecurityException: Sending SMS message: uid 10092 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
at **.**.****.MainActivity$3.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我检查过,但我拥有 list 中的权限,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-feature android:name="android.hardware.telephony"
android:required="true"/>
<application
android:exported="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我在互联网上搜索,但所有错误都与<uses-permission/>
有关。语法,你能帮我吗?
(以下内容摘自a blog post of mine about this)
现在没有得到你的许可的一个重要原因是你的项目有 targetSdkVersion
23 或更高,并且获得许可您所要求的内容是“危险的”。在 Android 6.0 中,这包括:
-
ACCEPT_HANDOVER
-
ACCESS_BACKGROUND_LOCATION
-
ACCESS_MEDIA_LOCATION
-
ACTIVITY_RECOGNITION
-
ANSWER_PHONE_CALLS
-
ACCESS_COARSE_LOCATION
-
ACCESS_FINE_LOCATION
-
ADD_VOICEMAIL
-
BODY_SENSORS
-
CALL_PHONE
-
CAMERA
-
GET_ACCOUNTS
-
PROCESS_OUTGOING_CALLS
-
READ_CALENDAR
-
READ_CALL_LOG
-
READ_CELL_BROADCASTS
-
READ_CONTACTS
-
READ_EXTERNAL_STORAGE
-
READ_PHONE_STATE
-
READ_SMS
-
RECEIVE_MMS
-
RECEIVE_SMS
-
RECEIVE_WAP_PUSH
-
RECORD_AUDIO
-
SEND_SMS
-
USE_SIP
-
WRITE_CALENDAR
-
WRITE_CALL_LOG
-
WRITE_CONTACTS
-
WRITE_EXTERNAL_STORAGE
对于这些权限,您不仅 targetSdkVersion
23+ 应用程序需要有<uses-permission>
元素,但你也有在运行时向 Android 6.0+ 上的用户请求这些权限设备,使用类似 checkSelfPermission()
的方法和 requestPermissions()
.
作为临时解决方法,请删除 targetSdkVersion
低于 23。
但是,最终,您将有理由想要您的 targetSdkVersion
23岁或以上。到时候,你将需要调整您的应用程序以使用新的运行时权限系统。Android 文档有 a page dedicated to this topic .
我是一名优秀的程序员,十分优秀!