Reputation: 409
I have created a broad cast receiver and alarm manager (the following code):
AlarmManager
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 11);
cal.set(Calendar.MINUTE, 18);
cal.set(Calendar.SECOND, 00);
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
MyReciever
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("SERVICE RECIEVED");
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService
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("static-access")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
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,
"Alarm", 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(),
"Alarm", "Open the app now",
pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
Manifest
<service
android:name=".MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
The issue I am having is that the Alarm Manager is running and calling the MyReceiver class but nothing is happening as it should. I know that its calling the function because in the log cat the following is displaying:
11-18 11:18:00.415: I/ActivityManager(765): START u0 {flg=0x4 cmp=pacakge/.MyReceiver (has extras)} from pid -1
The package value is the package name.
Can someone please explain where its going wrong? The alarm call works but the functions don't run.
Upvotes: 0
Views: 124
Reputation: 3776
onStart
was deprecated in API 5.
Change it to onStartCommand
. Follow this link
Hope it helps
Upvotes: 0
Reputation: 1435
Try Using
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
instead of
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 1