Вопрос

In my application I let user to buy application based of different plans, such as 2 hours free, 1 day, 1 week, and etc.

I want to don't let user use the application after expiry time. I created a service and set alarm in main activity and in service I have told that after expiry time open main activity.

The code of setting alarm in main activity:

private void updateService(){
        Intent myIntent = new Intent(DRMActivity.this, AlarmService.class);
        pendingIntent = PendingIntent.getService(DRMActivity.this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 30);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

        Toast.makeText(DRMActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
    }

The code of service:

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Intent myIntent = new Intent(getBaseContext(), DRMActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Toast.makeText(this, "AlarmService.onStart()", Toast.LENGTH_LONG).show();

        getApplication().startActivity(myIntent);
    }

So far everything is OK, even if my main activity is destroyed, service can run it again. But I think in android, unfortunately, application is built from several activities, therefore, from an activity we can't stop the application. What I mean? We have finish() method which will terminate the activity but it can't close the whole of application. Therefore, after terminating an activity of application, previous activity of that application will show.

So, back to my story. after for example 30 seconds (according to above code) service will run my main activity. But what will happen if user easily click back button? s/he can come back to previous activity of application and continue his usage.

One good suggestion is creating shared preferences and in service I change a flag and in onStart() of each activity I check it. I think it works but the bad thing is I am working on a wrapper that it should be add to other projects and I can't ask other developers to add this or check that.

What is your suggestion? is there anyway to don't let user to continue using application after expiry time?

=======> UPDATE:

For future reference. I Used FLAG_ACTIVITY_CLEAR_TOP and the problem is solved. Now, even user clicks back button all activities between Main activity and Last activity will clear. According to its documentation:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

Это было полезно?

Решение

Three possibilities:

  1. Override onBackPressed() to do nothing.
  2. Remove that Activity from the back stack altogether by adding a flag for FLAG_ACTIVITY_NO_HISTORY to the Intent that originally started it.
  3. Commit some data to SharedPreferences and check for it in onResume when user goes back. If the data says it's expired, then disable whatever functionality you want to.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top