4

i tired this a few months ago but failed.
what i'm trying to do is count the number of times the user unlocks his phone and show it on the screen but i'm getting vague numbers each time i unlock the phone.
my code is a follows.
My main activity oncreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(getApplicationContext(), LockService.class));
    TextView ticker;
    ticker = (TextView) findViewById(R.id.textView);
    ticker.setText(String.valueOf(Tick.tick));
    Log.e("but this is awesome ", String.valueOf(Tick.tick));
}

The Service class

public class LockService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_USER_PRESENT);
    final BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
    return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
    LockService getService() {
        return LockService.this;
    }
}

} The BroadcastReceiver Class

public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;


    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.e("test", "onReceive");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            wasScreenOn = false;
            Log.e("test", "wasScreenOn" + wasScreenOn);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            wasScreenOn = true;
            Log.e("test", "wasScreenOn and user present" + wasScreenOn);
        } else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            Tick.tick ++;
            Log.e("test", "userpresent" + Tick.tick);
        }
    }

}

please help me understand what i'm doing wrong

pavitran
  • 814
  • 1
  • 10
  • 25

1 Answers1

1

I believe what's happening is this: each time you open your activity, you call start service. So even if your service is already running, onStartCommand is being called. This way, you register your broadcast multiple times, and then when you unlock your device your counters are being incremented for each time you reciver has been registered. You should do one of these options: 1. Define your recievers in your manifest so you won't have to deal with registration and unregisteration each time. 2. Register you recievers in your service onCreate instead onStartCommand. Also, make sure you unregister them in your service onDestroy.

Shalev Moyal
  • 644
  • 5
  • 12
  • Thank you so much!i don't know how to do it the first way so i took the second approach and it worked. – pavitran May 22 '16 at 09:20
  • so the app count's the number of times the phone has been unlocked but when i leave the phone unused for a while it comes back to zero can you tell me the reason for that? – pavitran Jun 03 '16 at 12:38
  • Yes, after a while, the android os removes your app from the memory and Tick class is being deleted. You should save tick into SharedPreferences and edit it each time. This way it will not be deleted. – Shalev Moyal Jun 03 '16 at 13:35
  • so i have to declare a SharedPreferences in oncreate() of my activity and add my tick integer to it whenever onpause() is called and retrieve it again in oncreate()? – pavitran Jun 04 '16 at 12:39
  • No... first, delete the Tick class. as I said before, using static variables is not the right way to do it. When you receive a broadcast, get you last Tick count from sharedpreferences (put 0 as default value). then add +1 to it and save it to your shared preferences. in your activity, just load the ticks from sharedpreferences and set it tour textview. i think you should do it in your onResume. – Shalev Moyal Jun 04 '16 at 21:48
  • thank you very much it works.Now the only feature i want is ,i want the ticker to reset after every 24hours.can you tell me how i can implement this? – pavitran Jun 06 '16 at 06:29
  • 1
    You should use an Alarm broadcast receiver to scheduale a task like this. Take a look at this tutorial on how to create an alarm clock (just take what you need from there): http://javapapers.com/android/android-alarm-clock-tutorial/ – Shalev Moyal Jun 06 '16 at 14:33