我正在嘗試創建一個按鈕來發送短信到特定的號碼。然而,即使Android Studio沒有問題,按下按鈕時,應用程序也會在仿真器和物理設備上崩潰。發送短信與SmsManager不斷崩潰
錯誤沒有意義,因爲AndroidManifest.xml提供了正確的權限。
任何想法?提前致謝。
MainActivity.java
package saluta.salutapanicandreporter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button panicButton = (Button) findViewById(R.id.panicButton);
panicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("111-111-111", null, "RED ALERT. Reply SAFE when safe", null, null);
}
});
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="saluta.salutapanicandreporter">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_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>
<receiver android:name=".IncomingSms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
錯誤
08-10 17:52:55.723 2436-2436/saluta.salutapanicandreporter E/AndroidRuntime: FATAL EXCEPTION: main
Process: saluta.salutapanicandreporter, PID: 2436
java.lang.SecurityException: Sending SMS message: uid 10079 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1683)
at android.os.Parcel.readException(Parcel.java:1636)
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 saluta.salutapanicandreporter.MainActivity.sendSMS(MainActivity.java:16)
at saluta.salutapanicandreporter.MainActivity.access$000(MainActivity.java:11)
at saluta.salutapanicandreporter.MainActivity$1.onClick(MainActivity.java:31)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
請張貼錯誤。 –
'java.lang.SecurityException異常:發送短信:UID 10079沒有android.permission.SEND_SMS.' –