简体   繁体   中英

how to make an android app expire after a fixed amount of time?

Hi I want that the application that I have developed to stop working after like 5 days. In short I want to time bomb my android app Can someone tell me how to do this programmatically in android studio? Any possible codes to suit this? Thanks..

I am using this code to expire trial version after 26th March.

 String currentTime = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
        Log.d("timeStamp", currentTime);
        Calendar date = new GregorianCalendar(2016, Calendar.MARCH, 26);
        date.add(Calendar.DAY_OF_WEEK, 0);
        String expireTime = new SimpleDateFormat("yyyyMMdd").format(date.getTime());

        int intcurrentTime = Integer.parseInt(currentTime);
        int intexpireTime = Integer.parseInt(expireTime);


        if(intcurrentTime == intexpireTime || intcurrentTime  > intcurrentTime  ) {

            //logic to set off the features of app
            tvJ2.setText("Trial period expired!!");
}

Well,

if you know the time of publications then you can do something (silly) as

class MainActivity extends Activity {
    public static final long DESTROY_APP_TH = 432000000;

     @Override
     protected void onCreate(Bundle savedInstanceState){
           PackageManager pm = context.getPackageManager();
           PackageInfo pi= pm.getPackageInfo(context.getPackageName(), 0);
           long publishTimeInMilli = pi.firstInstallTime;

          long now = System.currentTimeMillis();
          if(now - publishTimeInMilli) > DESTROY_APP_TH) {
             //just finish the the activity (and thus the app) or do something else
             finish();
          }
     }

Use the following method to get the timestamp of the moment, when the app was installed for the first time:

private static long getFirstInstallTime(Context context) throws
        PackageManager.NameNotFoundException {

    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
    return info.firstInstallTime;
}

Now you can calculate the elapsed time and compare it with your desired app lifetime:

    long now = System.currentTimeMillis();
    if (now - getFirstInstallTime(getApplicationContext()) > 
                                TimeUnit.DAYS.toMillis(5)) {
        sendPoisonPill();
    }

Note: sendPoisonPill method is your implementation of 'time bomb'

I don't understand why do you want to do that, but anyway, you could create a String with a date and save it to SharedPreferences . In your main activity, before doing anything you compare the actual date with the date that you have saved in SharedPreferences (the saved date could be the first time that the application was opened or a prefixed date) and if the actual date is 5 days or more after your saved date, you can display a blank activity or whatever you want.

I have erite for you a code, where the first time you open the app the system saves the date and adds 5 days more. Then every time you open again the app you compare the saved date with the actual date, and if the actual date is after your saved date (it means that it has passed 5 days) you do whatever you want

class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState){
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      if (!prefs.getBoolean("firstRun", true) {
         SharedPreferences.Editor editor = prefs.edit();
         Calendar c = Calendar.getInstance();
         c.setTime(new Date());
         c.add(Calendar.DATE, 5);  // number of days to add
         String date = sdf.format(c.getTime());  // dt is now the new date
         editor.putBoolean("firstRun", true);
         editor.putString("valid_until", date).apply();
      }
      else if(new Date().after(sdf.parse(prefs.getString("valid_until","")))) {
         //Show whatever you want. Or finish the activity calling to finish();
      }
 }

You can use AlarmManager to send an intent to your app at the specified time. Have that intent handler close your app.

Please bear in mind, however, that depending on what exactly you want to do, this is, likely, not a good thing. An Android app running (ie - has a process that gets scheduled by the kernel), open (ie - has a view that the user can change to) or active (ie - it is the foreground view) are almost independent states. You need to specify which is what you want to expire.

您可以创建带有subscription的应用,这样5天后订阅到期并且应用变得无用(无法启动)

In my implementation, I will add 1 variable to SharedPreference. for example firstRun timestamp. Each time the app launch, I will get that variable and check it with the current time of the app. It is longer than 5 days (your setting), so terminate the app. But this way can't help if users change system time in their device.

The safer way is you should send this variable firstRun to your server. And each launching time, the app will request server and check condition inside your server.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM