我是Android的新手。当前我制作的简单应用程序从一个模拟器接收消息并将其发送到另一个模拟器。我无法在第二个模拟器中接收消息。这是我的代码。 MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_send);
final EditText e1 = (EditText) findViewById(R.id.editText);
final EditText e2 = (EditText) findViewById(R.id.editText2);
Button b1 = (Button) findViewById(R.id.MessageSend);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(e1.getText().toString(), null, e2.getText().toString(), null, null);
}
});
}
2.ManifestFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vap.telephonysecond" >
<uses-permission android:name="android.permission.SEND_SMS" />
<application
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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity>
android:name=".smsSend"
android:label="@string/title_activity_sms_send" >
</activity>
</application>
</manifest>
3.Log
10-01 20:40:40.604 23793-23815/com.vap.telephonysecond E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xac078100
10-01 20:40:56.802 23793-23793/com.vap.telephonysecond E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vap.telephonysecond, PID: 23793
java.lang.SecurityException: Sending SMS message: uid 10058 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 com.vap.telephonysecond.smsSend$1.onClick(smsSend.java:27)
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)
我使用5556或5554号码发送短信
答案 0 :(得分:0)
在您的清单文件中,<uses-permission android:name="android.permission.SEND_SMS" />
应该超出<application>
标记。将其保留在顶层,<manifest>
和</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vap.telephonysecond" >
<uses-permission android:name="android.permission.SEND_SMS" />
<application
...
如果仍然无法正常工作,请清理并重建您的项目。已知该解决方案有效。请参阅:Error sending text message Android