Well in my app I am trying the following functionality. I want depending on the date and the time, the user to receiver some notifications.
For example:
- September 9, 19.13 receive notification with message1
- September 10, 07.30, with message 2,
- same day, but 11.50, with message 3 and so on...
I used an alarm and a push bar notification but it worked only for the first one. So I serached for that and is is stated that I must use a repeating alarm manager.
Before I post my code I want to clarify some things: 1) I am thinking it that it should work like that: i must check every 1-2 min, what time is it, fetch my next "time" from an array that events are stored, and set ana alarm for that, right? Then given that this bynch of code will run every 1-2 minutes, i will check again, fetch the next event,set alarm for that time and so on.
Am I correct?
So to begin with, i am trying to implement a repeating alarm manager which every 1 min will display me a Toast message (If I do this and replacing the toast message with get_next_event() and set_next_notification() will do the job I think. - These function are working fine in my project with only one alarm being set).
But problem is that when I am starting my service I see nothing.
here is my code:
Alarm.java
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Starting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
Toast.makeText(context, "Setting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 , pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
YourService.java
public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
Toast.makeText(YourService.this, "Service Created", Toast.LENGTH_LONG).show(); // For example
}
public void onStart(Context context,Intent intent, int startId)
{
Toast.makeText(YourService.this, "Setting from Service", Toast.LENGTH_LONG).show(); // For example
alarm.SetAlarm(context);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
DemoActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Toast.makeText(ServicesDemo.this, "Button Pressed", Toast.LENGTH_LONG).show(); // For example
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, YourService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, YourService.class));
break;
}
}
So i press the button, I see that "button is pressed" , i see that "service created" but then none of the toasts that alarm started are being shown. And of course, I see nothing every 1 min.
here is my manifest.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".ServicesDemo" 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:enabled="true" android:name=".YourService" />
<receiver android:process=":remote" android:name="Alarm"></receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
</manifest>
So what do I need to change in my code or in the manifest?