java.langSecurityException:发送短信:uid 10057没有android.permission.SEND_SMS

时间:2015-09-12 20:53:14

标签: android

我正在尝试构建一个简单的应用程序来发送消息,但它给出错误:

  

java.langSecurityException:发送短信:uid 10057没有   have`android.permission.SEND_SMS

即使我在清单中添加了android.permission.SEND_SMS,也可以帮我解决这个问题

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>

2 个答案:

答案 0 :(得分:0)

在我的情况下,我将目标sdk更改为19,并且还发现您没有在清单中定义uses-sdk ...!

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

答案 1 :(得分:0)

尝试更改使用权限语法,如下所示:

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