簡體   English   中英

固定時間后如何使android應用過期?

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

嗨,我希望我開發的應用程序在5天后停止工作。 簡而言之,我想定時炸彈我的android應用程序,有人可以告訴我如何在android studio中以編程方式執行此操作嗎? 有任何適合的代碼嗎? 謝謝..

我正在使用此代碼在3月26日之后使試用版過期。

 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!!");
}

好,

如果您知道出版時間,則可以(傻)做一些

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();
          }
     }

使用以下方法獲取首次安裝該應用程序時的時間戳記:

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

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

現在,您可以計算經過的時間,並將其與所需的應用程序壽命進行比較:

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

注意:sendPoisonPill方法是您實現的“定時炸彈”

我不明白為什么要這樣做,但是無論如何,您都可以創建一個帶有日期的String並將其保存到SharedPreferences 在您的主要活動中,在進行任何操作之前,您先將實際日期與您在SharedPreferences中保存的日期進行比較(保存的日期可以是首次打開應用程序或帶前綴的日期),並且實際日期是5天還是保存日期之后,您可以顯示空白活動或任何您想要的內容。

我為您准備了一個代碼,第一次打開該應用程序時,系統會保存日期並增加5天。 然后,每次再次打開該應用程序時,您都會將保存的日期與實際的日期進行比較,如果實際的日期在保存的日期之后(這意味着它已經過去了5天),則您可以執行任何操作

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();
      }
 }

您可以使用AlarmManager在指定的時間向您的應用發送意圖。 讓該意圖處理程序關閉您的應用程序。

但是請記住,根據您的實際意願,這可能不是一件好事。 運行(即-具有由內核調度的進程),打開(即-具有用戶可以更改的視圖)或活動(即-這是前景視圖)的Android應用幾乎是獨立的狀態。 您需要指定要過期的內容。

您可以創建帶有subscription的應用,這樣5天后訂閱到期並且應用變得無用(無法啟動)

在我的實現中,我將向SharedPreference添加1個變量。 例如firstRun時間戳。 每次啟動應用程序時,我都會獲取該變量並與應用程序的當前時間進行檢查。 超過5天(您的設置),請終止該應用。 但是,如果用戶更改設備中的系統時間,這種方法將無濟於事。

比較安全的方法是,應將此變量firstRun發送到服務器。 並且每次啟動時,應用程序都會請求服務器並檢查服務器內部的狀況。

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM