Skip to content Skip to sidebar Skip to footer

Android Activity Comes To Foreground After Alarm Manager

I have application, which makes event in alarm manager, and at specific time its called. Code looks like this Intent intent = new Intent(this, AlarmActivity.class); pendingIntent =

Solution 1:

You should do all of this in a BroadCastReceiver. There is no UI, and there is a Context variable passed on to the Receiver's onReceive() method which allows you to basically do anything the Activity does, without having an actual UI. This means that you can set the ringer, show the status bar notification, etc. Your BroadcastReceiver class should look something like:

publicclassAlarmBroadcastReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
    //Change ringer mode//Add notification in status bar//Other boring stuff here...
    Toast.makeText(context,"Finishing",2000).show();
    }
}

Note that for your Toast, the variable named context is used.

And your AlarmManager code should look something like this:

Intentintent=newIntent(this, AlarmBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,req_code, intent, PendingIntent.FLAG_CANCEL_CURRENT);    
AlarmManageram= (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*7,
                    pendingIntent);

Your manifest should have this:

<receiverandroid:name=".AlarmBroadcastReceiver" ></receiver>

Solution 2:

Add this line to the Activity in your AndroidManifest

android:theme="@android:style/Theme.NoDisplay"

and you have an Activity with nothing to display. Since you are already calling finish(); in your code, it will look like it is running in background.

Post a Comment for "Android Activity Comes To Foreground After Alarm Manager"