我正在尝试从一个模拟器向另一个模拟器发送消息(SMS):
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eheio.com.exo2">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
发送消息方法:
public void sendMessage(View view) {
EditText number = findViewById(R.id.number);
EditText message = findViewById(R.id.message);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number.getText().toString(), null, message.getText().toString(), null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
我收到以下异常:
java.lang.SecurityException:正在发送SMS消息:uid 10082没有android.permission.SEND_SMS。
答案 0 :(得分:1)
对于初学者,您可以执行以下操作来检查授予了应用程序哪些权限:
adb shell dumpsys package <your package>
,您会看到类似的内容:
grantedPermissions:
android.permission.MANAGE_ACCOUNTS
android.permission.WRITE_SYNC_SETTINGS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.SEND_SMS
权限为dangerous
保护级别。这意味着从API 23开始,您需要提示用户接受许可。在清单中声明其使用是不够的。您可以在这里阅读有关内容:
https://developer.android.com/guide/topics/permissions/overview#dangerous-permission-prompt