Search code examples
androidandroid-intentandroid-activityalarmmanager

Trying to learn how to use intent and broadcast to kill my application. Why is it not dying?


I am almost finished with this game I have been making. It is a toy app and is only serving to teach me. So I call it toyapp.

I am trying to use what I learned about alarmmanager, intent, and broadcast to kill my application. I figured out how to get my alarm to go 10 seconds and then print a message to a widget. This was based on the following tutorial here.

It was my thinking that I could just call this up, warn the user I am about to kill this application and then close it out. However, it appears that the AlarmReceiver is not being called at all? My game just keeps playing.

The code I have modified is below for my toyapp.

public void timingService(Context context, Activity activity){
    // get a Calendar object with current time
     Calendar cal = Calendar.getInstance();

 // add 5 minutes to the calendar object
 cal.add(Calendar.SECOND, 5);

 Intent intent = new Intent(context, AlarmReciever.class);
 intent.putExtra("alarm_message", "Game will exit soon");


 // In reality, you would want to have a static variable for the request code instead of 192837
 PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 // Get the AlarmManager service
 AlarmManager am = (AlarmManager) activity.getSystemService(context.ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

}

Now, put my Receiver code in my activity class. Which is as follows:

public class AlarmReciever extends BroadcastReceiver{


        @Override
        public void onReceive(Context context, Intent intent) {
            try{
                Bundle bundle = intent.getExtras();
                String message = bundle.getString("alarm_message");
                Toast.makeText(context, message, Toast.LENGTH_LONG).show(); //Give message
                toyappActivity.this.finish(); //Exit the game

            }catch(Exception e){
                Toast.makeText(context, "There was an error somewhere", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }


    }//End of AlarmReciever

My inspiration for this approach was the following that I read here.

What I eventually want to do is simple. I will have an option to play the game with or without the alarm. I plan on reseting the alarmmanager with the accelerometer sensor if movement is detected. Otherwise, it will just countdown and then cancel the game.

I suspect I am not understanding intents directly, but this is my crashing point. I am so sorry if this seems stupid, but I have a rather bad cold right now and this seems like it should work to me. However, computers never lie, only our minds.

As always, you are all the best group of coders and developers. I learn so much from you all and your help is so much appreciated. Any dumb mistakes are my own, but I hope you let me off on that technicality.

Warm Regards, GeekyOmega


Solution

  • The Alarm is set properly: am.set(...), it should be called only once. But you would expect a message on you screen with:

    Toast.makeText(context, message, Toast.LENGTH_LONG).show(); //Give message
    

    The proces will refer to a :remote not sure the context what will be, as I remmeber the appContext. It seems you are trying to show a message from background and your GUI may block it. Try to remove (comment) that maeesage and let the

    toyappActivity.this.finish(); //Exit the game
    

    code run, just do a Log.d() if you want.