【问题标题】:java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND_SMS [duplicate]java.lang.SecurityException:发送短信:uid 10051 没有 android.permission.SEND_SMS [重复]
【发布时间】:2017-05-14 17:19:36
【问题描述】:

伙计们,我对sms manager 有关于预定短信的问题...在我使用的清单中

`<uses-permission android:name="android.permisson.SEND_SMS"/>`

但我收到此错误...我不知道为什么...我向您展示了我的 LOGCAT:

 FATAL EXCEPTION: main
                                                                       Process: com.example.alarm, PID: 31456
                                                                       java.lang.RuntimeException: Unable to start receiver com.example.alarm.AlarmReceiver: java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND_SMS.
                                                                           at android.app.ActivityThread.handleReceiver(ActivityThread.java:2615)
                                                                           at android.app.ActivityThread.access$1700(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                           at android.os.Looper.loop(Looper.java:193)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5327)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND_SMS.
                                                                           at android.os.Parcel.readException(Parcel.java:1465)
                                                                           at android.os.Parcel.readException(Parcel.java:1419)
                                                                           at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:926)
                                                                           at android.telephony.SmsManager.sendTextMessage(SmsManager.java:156)
                                                                           at com.example.alarm.AlarmReceiver.onReceive(AlarmReceiver.java:24)
                                                                           at android.app.ActivityThread.handleReceiver(ActivityThread.java:2597)
                                                                           at android.app.ActivityThread.access$1700(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                           at android.os.Looper.loop(Looper.java:193) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5327) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 

以及关于接收器广播的代码:

public class AlarmReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        String phoneNumberReciver="123456";
        String message="blablabla";
        /*String SPhone =i.getStringExtra("exPhone");
        String SSms = i.getStringExtra("exSmS");*/
        //android.telephony.SmsManager sms= SmsManager.getDefault();
        //sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumberReciver, null, message, null, null);
        Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG);


    }

}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="com.example.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permisson.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alarm.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>
        <receiver android:name=".AlarmReceiver"/>
    </application>

</manifest>

谁能帮助我?提前谢谢大家!

【问题讨论】:

标签: java android smsmanager


【解决方案1】:

复制并粘贴此代码以发送短信服务即可

Button sendBtn = (Button)findViewById(R.id.senbtn);

sendBtn.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
        sendSMSMessage();
     }
  });
 }

 protected void sendSMSMessage() {
  phoneNo = txtphoneNo.getText().toString();
  message = txtMessage.getText().toString();

  if (ContextCompat.checkSelfPermission(this,
     Manifest.permission.SEND_SMS)
     != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
           Manifest.permission.SEND_SMS)) {
        } else {
           ActivityCompat.requestPermissions(this,
              new String[]{Manifest.permission.SEND_SMS},
              MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
  }
}

   @Override
  public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
  switch (requestCode) {
     case MY_PERMISSIONS_REQUEST_SEND_SMS: {
        if (grantResults.length > 0
           && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(phoneNo, null, message, null, null);
              Toast.makeText(getApplicationContext(), "SMS sent.", 
                 Toast.LENGTH_LONG).show();
        } else {
           Toast.makeText(getApplicationContext(), 
              "SMS faild, please try again.", Toast.LENGTH_LONG).show();
           return;
        }
     }
  }

}

将此添加到清单文件中:

<uses-permission android:name="android.permission.SEND_SMS"/>

【讨论】:

  • 请不要在没有解释的情况下发布代码。它应该做什么(我知道,但 OP 可以忽略它,未来的用户也是)
  • 为什么需要解释它清楚地显示了代码所说的内容。不要发表负面评论。
  • How to Ask,你可以看到这一行简洁是可以接受的,但更全面的解释更好。代码对你来说可能很清楚,但你不应该假设这将是每个人都清楚。 A 这不是否定的,这是一个建议,如果我想否定,我也会对答案投反对票。 Here 是一个有趣的帖子。
  • 复制并粘贴此代码以发送短信服务即可 这不是对您的代码的解释。您应该解释一下,这将检查运行时权限(对于 API > 6.0,应该只有一个)。
【解决方案2】:

第 1 步 您的明显权限是

&lt;uses-permission android:name="android.permisson.SEND_SMS"/&gt;

更新为

<uses-permission android:name="android.permission.SEND_SMS"/>

它的 android.permission.SEND_SMS 不是android.permisson.SEND_SMS

权限拼写错误

第二步

<receiver android:name=".AlarmReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

【讨论】:

  • 我一直想知道为什么在构建过程中没有警告
【解决方案3】:

现在没有获得您的许可的主要原因是您的项目的 targetSdkVersion 为 23 或更高,并且您请求的许可是“危险的”。在安卓 6.0 中 您可以手动授予权限:

const onPermissionHandle = async () => {
// We need to ask permission for Android only
if (Platform.OS === 'android') {
  // Calling the permission function
  alert(granted);
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.SEND_SMS,
    {
      title: 'Example App SEND_SMS Permission',
      message: 'Example App needs access to your SEND_SMS',
    },
  );
   alert(granted);
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
    // Permission Granted
    alert('You can use the SEND_SMS');
  } else {
    // Permission Denied
    alert('SEND_SMS Permission Denied');
  }
} else {
  alert('You can use the SEND_SMS ');
}

};

【讨论】:

    【解决方案4】:

    您需要为 OS marshmallow 及更高版本授予运行时权限

    通过这个link

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              //SEE NEXT STEP
    
    }else{
    
       sendSms();
    
    }
    
        if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                      //SEE NEXT STEP
    
                    } else {
    
                        sendSMS();
    
                    }
    
    if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
                       //show some description about this permission to the user as a dialog, toast or in Snackbar
    
                    } else {
    
                        //request for the permission
    
                    }
    
    @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Snackbar.make(findViewById(R.id.rl), "Permission Granted",
                        Snackbar.LENGTH_LONG).show();
                sendSMS();
    
            } else {
    
               Snackbar.make(findViewById(R.id.rl), "Permission denied",
                        Snackbar.LENGTH_LONG).show();
    
            }
        }
    

    【讨论】:

    • 你可以提供运行时权限的官方文档,这也很好解释。 @Markus,查看副本以了解您的问题
    • 它的 android.permission.SEND_SMS 不是 android.permisson.SEND_SMS
    【解决方案5】:

    经过几个小时的调试,我发现在xml文件中添加这个权限不再产生错误了:

    <uses-permission-sdk-23 android:name="android.permission.READ_PHONE_STATE"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-24
      • 2019-03-18
      • 1970-01-01
      • 2012-02-18
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多