Reputation: 5040
I am trying to create an app for my personal use. I want to send message to one number when by bettery percentage goes below 5%.
I am getting the battery level and trying to send SMS using SmsManager like below.
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);
}
The problem is when I comment the Message part, it works fine and shows the battery percentage. But if i un-comment that part and try to send message i get the following error.
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
But actually i have permission to send SMS.
<?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>
Can somebody tell me what is wrong with my code? Thank you :)
Upvotes: 1
Views: 4948
Reputation: 10829
You made a small error in the <uses permission......>
. The spelling of android is incorrect.
It should be as below:
<uses-permission android:name="android.permission.SEND_SMS"/>
Upvotes: 4