0

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>
user2806748
  • 11
  • 2
  • 5

4 Answers4

0

You have to create one service,which going to send sms after 5 min.

In Broadcastreceiver call your service.

          public class MyReceiver extends BroadcastReceiver {

         public void onReceive(Context context, Intent intent) {

              TimerTask hourlyTask = new TimerTask () {
        @Override
             public void run () {
              Intent myIntent = new Intent(context,MyService.class); 
             startService(myIntent);
                }
               };

    // schedule the task to run starting now and then every hour...
         timer.schedule (hourlyTask,5000 * 60 * 1);
       }


    }
   }

in myservice class you write code to send sms in Onstart() method.

Ram
  • 2,532
  • 4
  • 22
  • 25
  • I tried this Broadcast receiver is not invoked on bootup after Android 3.0/3.1 version. So can you please suggest some other way? – user2806748 Sep 23 '13 at 11:08
  • @user2806748 Give full path to – Ram Sep 24 '13 at 05:17
  • Hi..Now it's working fine on Android 2.3 but it seems that it is not working on Android 4.0 + versions. Could yu please suggest what changes should I made so that it can work on Android 4.0 + versions also. Thanks a lot for your help. – user2806748 Sep 24 '13 at 08:31
  • I have one more requirement. Once SMS is sent we need to store this information so that when user reboots phone then SMS should not be sent. I cheked that for this purpose we can use "SharedPreferenceS" but these can be used inside Actitvity class only whereas I haven't used any activity class in this case? So What shoud I do to store these settings? Please help me. Thanks a lot. – user2806748 Sep 24 '13 at 08:59
  • @user2806748 U can use shared pref. in receiver class. SharedPreferences prefs = context.getSharedPreferences("**.***.***", Context.MODE_PRIVATE); – Ram Sep 24 '13 at 09:28
  • Is it possible to use Shared preferences in some other class also? Also what changes I should made so that it can work on Android 4.0+ versions also. Thanks – user2806748 Sep 24 '13 at 09:36
  • @user2806748 ya..u can use the shared pref. data in other class also. – Ram Sep 24 '13 at 09:58
  • Can you please provide me some code snippet to use Shared preferences in other classes? I tried to find on net but couldn't find. – user2806748 Sep 24 '13 at 10:15
  • @user2806748 SharedPreferences prefs = MainActivity.this.getSharedPreferences("com.example.simcontact", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("Send_number",send_no); editor.commit(); //Include it in another class..... SharedPreferences prefs = getSharedPreferences("com.example.simcontact", Context.MODE_PRIVATE); send_no = prefs.getString("Send_number", ""); – Ram Sep 24 '13 at 10:21
  • Thanks for this info. But I don't have any Activity class in my app. So in this case I can't use SharedPrefs of other class (Activity in this case). – user2806748 Sep 24 '13 at 11:16
  • @user2806748 The above code i used the shared pref. in receiver class and service class only.Not in activity. – Ram Sep 24 '13 at 11:32
  • @user2806748 have you got result?. – Ram Sep 25 '13 at 11:38
  • Yes..Now it's working fine on Android 2.3. But not working on Android 4.0....Donno what should I do so that it can work on 4.0 also. If you have any idea please guide me..BTW thanks for all your help.. – user2806748 Sep 25 '13 at 13:44
0

You should start a service from your onReceive method in your (BOOT_COMPLETED) BroadcastReceiver.

See here:

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#startingservices_alarmmanager

Also note:

If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.

Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.

Damian
  • 8,062
  • 4
  • 42
  • 43
  • Thanks for your suggestion but in My requirement I don't want user to start my app manually. It's kind of sales tracker application so SMS should be sent in background when user boots phone first time. Could you please suggest some other way to achieve this requirement? – user2806748 Sep 23 '13 at 11:07
  • I think you might be out of luck. There is no hook for when your app is installed and in order to act on BOOT_COMPLETED you need to run your app at least once. http://developer.android.com/about/versions/android-3.1.html#launchcontrols – Damian Sep 23 '13 at 11:24
  • I have one more requirement. Once SMS is sent we need to store this information so that when user reboots phone then SMS should not be sent. I cheked that for this purpose we can use "SharedPreferenceS" but these can be used inside Actitvity class only whereas I haven't used any activity class in this case? So What shoud I do to store these settings? Please help me. Thanks a lot. – user2806748 Sep 24 '13 at 09:27
0

Use pending Intent and Alarm Manager for send message after 5 minutes of boot up.

Intent i = new Intent(this,Shedulesms.class);
Bundle msg = new Bundle();
msg .putString("number", "1234567890");
msg .putString("message", "This is test message");
i.putExtras(msg );
PendingIntent pu=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.MINUTE,cal.get(Calendar.MINUTE)+5);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pu);

in Shedulesms.java

enter code here

SmsManager sms=SmsManager.getDefault();
Bundle b=getIntent().getExtras();
String number=b.getString("number");
String msg=b.getString("message");
sms.sendTextMessage(number, null,msg, null, null);

Give Permission in android manifiest file

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

and register broadcast receiver in manifiest

Virender
  • 168
  • 10
  • @ Virendra, Thanks for your suggestion. But how will this code be invoked after first boot up. I registered Broadcast receiver to receive BOOTUP_COMPLETE event but it's not working after 3.1 version. Please correct me if I missed something and guide me in this regard. Thanks – user2806748 Sep 23 '13 at 11:12
0

As described in some answers in Trying to start a service on boot on Android, some phones have a fastboot-option. To support these phones, add

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

to your intent-filter.

Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76