I tried to do the same, but did not find a better way to rashly ask the activity manager, now, so as not to waste time using alarmManager and recieverto, set the pollin wake-up time so that when the phone sleeps it will not be woken up for your survey (suppose that the user uses applications when the screen is on and the phone is active) and polling like every 5 seconds or so here is a basic code example
Intent alarmIntent = new Intent(context, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), pendingIntent); public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) {
Using AlarmManager.RTC, make sure that the receiver only polls when the phone is awake.
source share