【发布时间】:2019-03-18 04:47:27
【问题描述】:
我正在尝试从一个模拟器向另一个模拟器发送消息 (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:发送短信:uid 10082 没有 android.permission.SEND_SMS。
【问题讨论】:
-
如果你的目标是API23及以上是危险权限,需要运行时权限请求。
标签: java android mobile android-emulator