Reputation: 357
I have a problem and I'm hoping something could kindly point out why it is occuring. I hard-code the date that I want the alarm to fire, and when running the app for the first time, it does only fire at that instance in time. However, once the alarm has been fired, every time the app is open, the alarm continually fires and displays a notification. This is just a small app test that will be used as part of a bigger application and was hoping someone could help me please
package com.example.alarmtest;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 12);
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 26 );
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
}
package com.example.alarmtest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyAlarmService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
package com.example.alarmtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarmtest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alarmtest.NextActivity"
android:label="Next Screen">
</activity>
<service android:name="com.example.alarmtest.MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
</application>
</manifest>
Upvotes: 1
Views: 2257
Reputation: 911
Okay so I had similar situation I will go through possible fixes but first lets examine why this happens.
In my case, when you open app it sets alarm for that day, for eg, if alarm being set is before current time it will be fired immediately after setting. Since I showed notifications in alarm receiver, most of the times when I opened app, morning alarms where popping up. Which verifies above condition.
To stop this, either put a check on setting alarms for only future. Or put this check in the alarm receiver that if alarm time is less than current time, dont fire notification.
I set the alarm time in milliseconds in intent before setting alarm as long extra:
long timestamp=1111882929; // your alarm timestamp
alarmIntent.putExtra("timestamp",timestamp);
//... setAlarm...
When you receive intent in onRecieve
of AlarmReceiver
(in your case its a Service
class) put a check before showing notifications like this:
if(intent.getLongExtra("timestamp",0)> System.currentTimeMillis())
{
showNotification(intent);
}
// else do nothing
Also, if you set an alarm with same pending request code for eg:
int CODE=112; // any code
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast( context, CODE, alarmIntent, 0 );
This will set alarm with code 112
, setting alarm with this code again will override it again.
Upvotes: 1
Reputation: 7797
I think your MainActivity
onCreate()
method will be invoked most of the times you reopen your app. Thus, you will set an alarm multiple times. Later alarms will be set in the past (I assume you set your first alarm for current time) and will be fired immediately.
Upvotes: 2