Nakshatran
Nakshatran

Reputation: 426

Alarm is not repeating at the mentioned time why?

I am trying to repeat the alarm daily at 9.am.But the alarm is not repeating at the right time, ie., it just showing the toast after launching of the app.why?

suggestions please!..

Thanks for your precious time..

Please find my sources for reference

AndroidAlarmService.java

public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;

@Override

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Intent myIntent = new Intent(AndroidAlarmService.this, RepeatingAlarm.class);
 pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

 AlarmManager aM = (AlarmManager)getSystemService(ALARM_SERVICE);

 Calendar c=Calendar.getInstance();
 c.set(Calendar.HOUR_OF_DAY, 9);
 c.set(Calendar.MINUTE, 0);
 c.set(Calendar.SECOND, 0);


 PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, new Intent(getApplicationContext(),AndroidAlarmService.class),PendingIntent.FLAG_UPDATE_CURRENT);
 aM.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
 }
 }

RepeatingAlarm.java

public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        Toast.makeText(context,"Alarm Started.....", Toast.LENGTH_LONG).show();

    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);

        }
    }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.exercise.AndroidAlarmService"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".AndroidAlarmService"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyAlarmService" />
</application>
</manifest>

Upvotes: 0

Views: 142

Answers (1)

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

As your are creating a Service PendingIntent that why BroadcastReceiver is not called

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Intent myIntent = new Intent(AndroidAlarmService.this, RepeatingAlarm.class);
 pendingIntent = PendingIntent.getBroadcast(AndroidAlarmService.this, 0, myIntent, 0);

 AlarmManager aM = (AlarmManager)getSystemService(ALARM_SERVICE);

 Calendar c=Calendar.getInstance();
 c.set(Calendar.HOUR_OF_DAY, 9);
 c.set(Calendar.MINUTE, 0);
 c.set(Calendar.SECOND, 0);
 PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, new Intent(getApplicationContext(),AndroidAlarmService.class),PendingIntent.FLAG_UPDATE_CURRENT);
 aM.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
 }
 }

Upvotes: 1

Related Questions