我正在尝试创建一个按钮,以便将SMS发送到特定号码。但是,即使Android Studio没有问题,按下按钮时应用程序也会在模拟器和物理设备上崩溃。
鉴于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)
答案 0 :(得分:0)
你在API&gt; = 23上进行测试吗?有一个名为“在运行时”的新权限流,你应该使用这个
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
检查
已授予的权限boolean permissionGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED;
另外,我不是舒尔,但是因为KitKat(?)设备上可能只有一个可以发送短信的应用程序(仍有多个可以读取)。你有一个操作系统设置选择器,如浏览器。当你有多个发送短信应用时,他们正在检查(应该......)它们被标记为默认并且能够发送短信。检查这个线索
答案 1 :(得分:0)
您可能正在Android M(6)或更高版本上测试您的应用程序?
如果是,则需要运行时权限。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// For device above MarshMallow
boolean permission = getSMSPermission();
if(permission) {
// If permission Already Granted
// Send You SMS here
}
}
else{
// Send Your SMS. You don't need Run time permission
}
public boolean getWritePermission(){
boolean hasPermission = (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 10);
}
return hasPermission;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 10: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
// Permission is Granted
// Send Your SMS here
}
}
}
}