Monday, 1 December 2014

Set Demo Period Expired

Demo Period Expired

Today i am going to show the tips about, how to set the demo period in Android Application?

There are several ways to do it, But very simple and perfect way is to use SharedPreferences,

1) Step

we have to create a SharedPreferences to keep the mandatory details about the application,

2) Step 

To save the current date (Installation Date) on it.

3) Step 

Check the condition for every wakeup of the application for expire period days with the current date,


This is the method to check the validation of 10 Days Demo

private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private final long oneday = 24 * 60 * 60 * 1000;

public void checkExpireDate() throws ParseException {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String installDate = preferences.getString("InstallDate", null);
if (installDate == null) {
SharedPreferences.Editor editor = preferences.edit();
Date now = new Date();
String dateString = formatter.format(now);
editor.putString("InstallDate", dateString);
editor.commit();
} else {
Date before = (Date) formatter.parse(installDate);
Date now = new Date();
long diff = now.getTime() - before.getTime();
long days = diff / oneday;
if (days > 10) {
AlertDialog.Builder ad= new AlertDialog.Builder(new ContextThemeWrapper(ReaderB.this, R.style.popup_theme));
ad.setTitle("Your App Is Exceeded the Trial Version");
ad.setMessage("Do You Want To Purchase?..,");
ad.setInverseBackgroundForced(false);
ad.setCancelable(false);
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// Direct to our site if the application is expired
@Override
public void onClick(DialogInterface dialog, int which) {
Intent fina=new Intent(ReaderB.this,DcsSite.class);
startActivity(fina);
finish();
}
});
ad.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
ad.show();
}
}
}

Style. xml file for Popup Theme 

 <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <color name="back_color">#ffffffff</color>

    <style name="popup_theme" parent="@android:style/Theme.Dialog">
        <item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
        <item name="android:windowBackground">@color/back_color</item>
        <item name="android:colorBackground">@color/back_color</item>
    </style>

Thats All..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

No comments:

Post a Comment