java.lang.SecurityException:发送短信:uid 10051没有android.permission.SEND_SMS

时间:2016-12-30 11:40:53

标签: java android smsmanager

伙计们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>

世界卫生组织可以帮助我吗?大家先谢谢大家!

5 个答案:

答案 0 :(得分:3)

复制并粘贴此代码,以便发送短信服务

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;
        }
     }
  }

}

在menifest文件中添加:

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

答案 1 :(得分:2)

第1步 您的明确许可是

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

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

其  android.permission.SEND_SMS不是android.permisson.SEND_SMS

权限拼写错误

<强>步骤2

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

答案 2 :(得分:0)

您需要为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();

        }
    }

答案 3 :(得分:0)

经过数小时的调试,我发现将此权限添加到xml文件中不再产生错误:

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

答案 4 :(得分:0)

当前未获得许可的主要原因是因为您的项目的targetSdkVersion为23或更高,并且您所请求的许可是“危险的”。在Android 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 ');
}

};