0

I have a game and I want to send a notification every few hours that the app was closed without opening it. To make these notifications isn't the problem, but I have no idea how to count the time that the app was not used? Should I save in local file like App Preference class? Or is there different way?

For example: I played the game and then closed it. I didn't touch the game for one day, and then I received a notification "Come back blah blah and play blah blah"

  • Preferences would be my first choice here, Simple and effective. – ci_ Mar 19 '15 at 11:13
  • have u got any control from the server on whether the app is connected or not? – eduyayo Mar 19 '15 at 11:13
  • refer this http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service/5528441#5528441 – Rince Thomas Mar 19 '15 at 11:16
  • "I want to send a notification every few hours" -> please consider the UX side of things as well. I would uninstall such an application, regardless of how good it actually is, if I receive pushy notifications every few hours, and I'm sure most people would as well. – 2Dee Mar 19 '15 at 11:21
  • @2Dee well of course. Not going to do it every few hours, but about once in 2 days if not used. – user4689356 Mar 19 '15 at 12:49

2 Answers2

0

Use AlarmManager to set alarm when you exit your app which you will receive in your receiver class like the below code

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID_XYZ, new Intent(
                        FILTER_TIME_ABC), PendingIntent.FLAG_UPDATE_CURRENT);

                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1*60*60*1000, 1*60*60*1000, pendingIntent);

And on your app start cancel all alarms using below code:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID_XYZ, new Intent(
                        FILTER_TIME_ABC), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Arun Kumar
  • 194
  • 8
0

If you use a service that's always running there's no need to use Alarmmanager. Handler is easier. Use Share preferences and Some Calculation like.

  // Calendar.getInstance().getTime() This is previous time 

                Date prevTime = Calendar.getInstance().getTime();
                Date currentTime = null;
                SimpleDateFormat formatter = new SimpleDateFormat(
                        "EEE MMM dd HH:mm:ss zzzz yyyy");
                currentTime = formatter.parse( get value from share preference);

                if (((prevTime.getTime() - currentTime.getTime())
                        / (60 * 1000) % 60) < 30)
                    {
   //Call Your function

        }

Hope this will help you

Fasiha
  • 488
  • 3
  • 11