Android android.permission.SEND_SMS无法正常运行

时间:2014-09-02 11:56:43

标签: android telephonymanager smsmanager

我正在尝试创建一个供个人使用的应用。当赌注百分比低于5%时,我想向一个号码发送消息。

我正在获取电池电量并尝试使用SmsManager发送短信,如下所示。

private void getBatteryPercentage() {
    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }
            batteryPercent.setText("Battery Level Remaining: " + level + "%");
            //if(level<=5 && notSent){
            //    notSent=false;
            //   SmsManager smsManager=SmsManager.getDefault();
            //  Log.d("Sending message to", "9999999999");
            //   smsManager.sendTextMessage("9999999999",null,"Battery percent: "+level+"%",null,null);
            // }
        }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

问题是当我评论消息部分时,它工作正常并显示电池百分比。但如果我取消评论该部分并尝试发送消息,我会收到以下错误。

      09-02 17:03:06.795  12849-12849/com.example.karthik.lowbattery E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.karthik.lowbattery, PID: 12849
      java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.example.karthik.lowbattery.MyActivity$1@4517e460



      Caused by: java.lang.SecurityException: Sending SMS message: uid 10268 does not have android.permission.SEND_SMS.
      at android.os.Parcel.readException

但实际上我有权发送短信。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.karthik.lowbattery" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="andriod.permission.SEND_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有人能告诉我我的代码有什么问题吗? 谢谢:))

1 个答案:

答案 0 :(得分:4)

您在<uses permission......>中犯了一个小错误。 android的拼写错误。

应该如下:

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