I want to send an SMS without user intervention so I crated a BroadcastReceiver
to solve this problem. This BroadcastReceiver
will receive "BOOT_COMPLETED"
notification at bootup. In the OnReceive
function we can send SMS (After 5 minutes of bootup). I tried this logic but it is not working.
I came to know from some other posts on this site that after Android 3.1+ we cannot use this kind of logic to meet this requirement. But I tried this logic on Android 2.3 and it seems that it is not working there either.
Please suggest to me how to solve this problem on Android 2.3 and Android 4.0+ versions. I don't want to create any UI for this requirement.
I am trying following piece of code.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.telephony.SmsManager;
import android.util.Log;
class MyCountDownTimer extends CountDownTimer {
private int no_of_attempts;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
no_of_attempts = 0;
}
@Override
public void onFinish() {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+9198104084753", null, "Test Message ", null, null);
} catch (Exception e) {
Log.d("BGSMS","SMS Sending failed..Try to resend SMS");
no_of_attempts++;
if (no_of_attempts <= 3)
{
long startTime = 5 * 1000;
long interval = 1 * 1000;
CountDownTimer countDownTimer;
countDownTimer = new MyCountDownTimer(startTime, interval);
countDownTimer.start();
}
}
}
@Override
public void onTick(long millisUntilFinished) {
}
}
public class SalestrackerReceiver extends BroadcastReceiver {
private CountDownTimer countDownTimer;
private final long startTime = 5 * 1000;
private final long interval = 1 * 1000;
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Anshul","Anshul Broadcast receiver received");
countDownTimer = new MyCountDownTimer(startTime, interval);
countDownTimer.start();
}
}
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sales_tracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="SalestrackerReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>