java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND_SMS

风流意气都作罢 提交于 2019-12-18 09:54:30

问题


guys i have problem with sms manager about sms scheduled...in manifest i used

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

but i get this error...i don't know why...i show you my 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) 

and code about receiver broadcast:

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


    }

}

THIS IS MY MANIFEST:

<?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>

WHO can help me? thanks in advance everybody!


回答1:


Step 1 your menifest permission is

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

update it with

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

its android.permission.SEND_SMS not android.permisson.SEND_SMS

permission spelling is wrong

step2

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



回答2:


Copy and paste this code for sending sms services will done.

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

}

add this in menifest file:

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



回答3:


you need to give runtime permission for OS marshmallow and above

go through this 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();

        }
    }



回答4:


After hours of debugging, I found out that adding this permission to the xml file does not produce the error anymore:

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


来源:https://stackoverflow.com/questions/41395680/java-lang-securityexception-sending-sms-message-uid-10051-does-not-have-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!