按钮发送短信时出错。安卓

时间:2016-04-07 08:37:33

标签: android android-studio

我是Android开发的新手。并尝试在按钮单击时发送预定义的短信。每当点击按钮错误显示java.lang.SecurityException:发送短信:用户10134没有android.permission.SEND_SMS.I已采取静态2数据类型

BigInteger phoneNumber = new BigInteger("9876543210");
String smsBody="Ping"; 

我的主要文件是: -

public void onClick(View v) {

    if(v==ping) {
        try {

            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNumber.toString(), null, smsBody.toString(), null, null);
            Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
                    Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(), "Your sms has failed...",
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }

    }

我的清单文件是

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

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

        <activity
            android:name=".SplashScreen"
            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=".Login_page" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity android:name=".Registration_page" />
        <activity android:name=".Booking_page" />
        <activity android:name=".admin_page" />
        <activity android:name=".User_page" />
        <activity android:name=".order" >
        </activity>
    </application>

</manifest>

请建议我在哪里错。

2 个答案:

答案 0 :(得分:2)

您必须先在应用程序标记之前声明用户权限标记。请参阅此安卓文档,了解Structure of the manifest file

并像这样更改您的清单文件:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/icc"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".SplashScreen"
            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=".Login_page" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

        <activity android:name=".Registration_page" />
        <activity android:name=".Booking_page" />
        <activity android:name=".admin_page" />
        <activity android:name=".User_page" />
        <activity android:name=".order" />
    </application>

</manifest>

答案 1 :(得分:1)

移动

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

出自

<application

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