0

I am trying to build a simple app to send message, but its giving error:

java.langSecurityException:Sending SMS message:uid 10057 does not have`android.permission.SEND_SMS

even though i have added android.permission.SEND_SMS in manifest, help me solve this problem

main-activity.java
package com.example.manju.helpme;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.Edit
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

/* called when user clicks ALERT! button*/

    public final static String EXTRA_MESSAGE = "com.example.HelpMe.Message";

    public void sendMessage(){


        String phoneNo = "5556";
        String message = "hi";

         try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "SMS failed, please try again.",
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
        Intent intent = new Intent(this,DisplayMessageActivity.class);
        String Message = e.toString();
        intent.putExtra(EXTRA_MESSAGE, Message);
        startActivity(intent);
    }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    Button btnAlert = (Button)findViewById(R.id.btn_Alert);
    Button btnAddGuardian = (Button)findViewById(R.id.btn_Add_Guardian);
    Button btnRemoveGuardian = (Button)findViewById(R.id.btn_Remove_Guardian);



    btnAlert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        sendMessage();
        }
    });

    btnAddGuardian.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


    btnRemoveGuardian.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //no inspection Simplifiable If Statement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}









Android Manifest.XML

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

<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".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>
    <activity
        android:name=".Guardians"
        android:label="@string/title_activity_guardians"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.manju.helpme.MainActivity" />
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
    </activity>
        </application>

</manifest>
Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47

2 Answers2

0

In my case, I changed the targeted sdk to 19 and also find that you didn't define the uses-sdk in the manifest...!

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
0

Try changing the uses-permission syntax like this:

<uses-permission android:name="android.permission.SEND_SMS"/>
Scorpion
  • 396
  • 2
  • 14