0

I need to schedule a task(send a text message) using Alarm Manager, and I need to send it repeatedly in a 1 minute interval.

I would really appreciate any help regarding the same. Thanks in advance.

Here is the code that I have written,

AndroidManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SMSScheduler"
        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>

<service android:name=".MyAlarmService" android:enabled="true" />

<receiver android:process=":remote" android:name=".MyReceiver" /> 

SMSScheduler.java

package com.example.smsmessaging;

public class SMSScheduler extends Activity {

    Button btnSendSMS;

    Calendar calendar = Calendar.getInstance();

    private PendingIntent pendingIntent;

    public void startNotification () {

        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(2015, 1, 13, 10, 12, 00); calendar.getTime();

        Intent myIntent = new Intent(SMSScheduler.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(SMSScheduler.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        long trigger = calendar.getTimeInMillis();
        long delay = 1*60*1000; // 1 minute delay needed

        // TEST CODE
        String time = calendar.getTime().toString();
        Log.v("TEST", time); 
        Toast.makeText(getBaseContext(), time, Toast.LENGTH_LONG).show();

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, trigger, delay, pendingIntent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsscheduler);

        btnSendSMS = (Button)findViewById(R.id.buttonSchedule);

        btnSendSMS.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startNotification();
            }
        });
    }
}

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
      int count = 1;
      Map<Integer, String> oMessages = new HashMap<Integer, String>();

        public void updateMessages() {

            oMessages.put(1, "TEST 1");
            oMessages.put(2, "TEST 2");
            oMessages.put(3, "TEST 3");
            oMessages.put(4, "TEST 4");
        }

        public String getMessage(Integer i) {
            return oMessages.get(i);
        }


    @Override
    public void onReceive(Context context, Intent intent)
    {
            // TODO Auto-generated method stub

        updateMessages();                 
        String phoneNumberReciver="123456789";// phone number to which SMS to be send
        String message=getMessage(count);// message to send

        SmsManager sms = SmsManager.getDefault(); 
        sms.sendTextMessage(phoneNumberReciver, null, message, null, null);

        // Show the toast  like in above screen shot
        Log.v("TEST", "Alarm Triggered and SMS Sent");
        Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
     }
}

SMSMessagingManifest.XML

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

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SMSScheduler"
            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>

    <service android:name=".MyAlarmService" android:enabled="true" />

    <receiver android:process=":remote" android:name=".MyReceiver" /> 


</manifest>
JNL
  • 4,683
  • 18
  • 29

2 Answers2

0

Your code seems ok expect "requestCode" parameter in PendingIntent.getBroadcast()

Could you try with a non-zero value:

Intent myIntent = new Intent(SMSScheduler.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(SMSScheduler.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

If it's not fix give us more details on what's wrong.

LaurentY
  • 7,495
  • 3
  • 37
  • 55
0

I think I found error. In setRepeating method replace trigger parameter by System.currentTimeMillis()

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), delay, pendingIntent);
LaurentY
  • 7,495
  • 3
  • 37
  • 55
  • I want the timer to start on a future time.. hence the trigger is set on the future date. – JNL Feb 13 '15 at 16:20
  • No. Still does not send the text message. I have a very similar application with sending Notifications. It works beautifully over there, but not while sending Text Messages. – JNL Feb 13 '15 at 16:29
  • Thanks for your time and help Laurent. Caught the bug in the AndroidManifest.xml file. – JNL Feb 13 '15 at 19:06