简体   繁体   中英

how to fix sms permission error android studio?

in this project, I want to send a location message to the destination number. I have added SEND_SMS in Manifest, I want this application to send a message when I press the button. but there is a problem in the code.

this is error in logcat


020-05-06 16:38:42.694 20708-20905/com.example.sos2 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.example.sos2, PID: 20708
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:353)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.SecurityException: Sending SMS message: uid 10218 does not have android.permission.SEND_SMS.
        at android.os.Parcel.readException(Parcel.java:1960)
        at android.os.Parcel.readException(Parcel.java:1899)
        at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:867)
        at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:367)
        at android.telephony.SmsManager.sendTextMessage(SmsManager.java:350)
        at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:792)
        at com.example.sos2.SendMessage1$SendMessageTask.doInBackground(SendMessage1.java:727)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
        at java.lang.Thread.run(Thread.java:764) 

this is my java class

lass SendMessageTask extends AsyncTask<Void, Integer, Void> { //I have changed the input from View to Void
        View view;
        String address,city,state,country,postal_code,known_name;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... view) {

            dTap = 0;
            for (int i = 1; i <= 40; i++) {
                try {
                    Thread.sleep(500);
                    presentTime=i*500;
                    if(i%2 == 0)
                    {
                        publishProgress(500*i/1000+10);
                    }
                 //   Log.d("hehe","thread is running");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (dTap == 1) {
                    dTap=0;
                   // Log.d("hehe","dtap is 1");
                    publishProgress(2);
                    return null;
                }
            }

            publishProgress(1);



          //  Log.d("hehe","starting recording");
//            Log.d("hehe","Send Message loc: "+finalAddress);
            startRecording();
            SharedPreferences sp = getSharedPreferences("Contacts", Context.MODE_PRIVATE);
            String s1 = sp.getString("1", "chooseContact1");
            String s2 = sp.getString("2", "chooseContact2");
            String s3 = sp.getString("3", "chooseContact3");
            String s4 = sp.getString("4", "chooseContact4");
            String s5 = sp.getString("5", "chooseContact5");
            SmsManager d = SmsManager.getDefault();

            SmsManager smsManager = SmsManager.getDefault();

            s1 = s1.replaceAll("\\s+","");
            s2 = s2.replaceAll("\\s+","");
            s3 = s3.replaceAll("\\s+","");
            s4 = s4.replaceAll("\\s+","");
            s5 = s5.replaceAll("\\s+","");

            if(finalAddress.equals("Hey.. I am in Danger. Please, help me ASAP!!")){
                publishProgress(500);
            }
//            Log.d("hehe",s1);
            if(!s1.equals("chooseContact1")) {
               // Toast.makeText(SendMessage1.this,"this is contact one "+ finalAddress,Toast.LENGTH_SHORT).show();
//                    Log.d("hehe",finalAddress+"this is sms 1");

                smsManager.sendTextMessage(s1,null,finalAddress,null,null);
//              Log.d("hehe","sending sms"+finalAddress);
            }
            if (!s2.equals("chooseContact2")) {
                smsManager.sendTextMessage(s2, null, finalAddress, null, null); //must be uncommented
            }
            if (!s3.equals("chooseContact3")) {
                smsManager.sendTextMessage(s3, null, finalAddress, null, null);
            }
            if (!s4.equals("chooseContact4")) {
                smsManager.sendTextMessage(s4, null, finalAddress, null, null);
            }
            if (!s5.equals("chooseContact5")) {
                smsManager.sendTextMessage(s5, null, finalAddress, null, null);
            }

            //Geocoder geoCoder ;

            //  Toast.makeText(this,ans +"this is the address",Toast.LENGTH_SHORT).show();


            return null;
        }

what should i change or i add to Fixx this code? help me guys I use SDK 23 in this project

You have to check permission also during runtime since API 23. User can revoke permissions for your app anytime, so you have to make sure, that you can still send SMS, before you execute your async task.

Try adding something like this before your async task:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
    // You have to ask for permission again
} else {
    // Granted, you can start you async task here
}

Docs: https://developer.android.com/training/permissions/requesting.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM