Reputation: 873
I have a simple dialog that should be shown if there is a new version of .apk
. The problem is that it shows every time the onResume()
method is called.
I was wondering if it is possible to only show this dialog once per month.
Below is the code of the Dialog
and the condition which tells me if there is a new .apk
.
public void onResume() {
super.onResume();
new getVersionName(getApplicationContext()).execute();
}
public class getVersionName extends AsyncTask<String, Integer, String> {
private Context mContext;
getVersionName(Context context){
mContext = context;
}
//Compare with the online version
@Override
protected String doInBackground(String... sUrl) {
// String path = Environment.getExternalStorageDirectory().getPath();
String path ="http://test.com/AndroidApp/test.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
String getVersion = bo.toString().substring(12, 17);
String getVersionFromUrl = BuildConfig.VERSION_NAME;
if (!getVersionFromUrl.equals(getVersion)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("It is a new version of this app");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int permissionExternalMemory = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
String url = "http://test.com/AndroidApp/test.v1.0.3.apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String getUrl = url.substring(34, 55);
request.setTitle(getUrl);
getMimeType(getUrl);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (permissionExternalMemory != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
STORAGE_PERMISSIONS,
1
);
}
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getUrl);
manager.enqueue(request);
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.show();
}
});
}
} catch (Exception e) {
Log.e("YourApp", "Well that didn't work out so well...");
Log.e("YourApp", e.getMessage());
}
return path;
}
@Override
protected void onPostExecute(String path) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive" );
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// mContext.startActivity(i);
}
}
Upvotes: 0
Views: 616
Reputation: 5149
The previous answer had the right idea but is executed incorrectly as it will not work after the last month in the year!
You could test to see if a month (30 days) has passed using the following method:
public boolean hasMonthPassed() {
long lastTimestamp = prefs.getLong("myPreferenceKey", 0);
long currentTimestamp = System.currentTimeMillis();
return (currentTimestamp - lastTimestamp) >= TimeUnit.DAYS.toMillis(30);
}
You can then use this in your class elsewhere:
if(hasMonthPassed()) {
// Your code here
// Set the preference
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("myPreferenceKey", System.currentTimeMillis());
editor.apply()
}
Alternatively, the previous answer can be fixed by updating as follows:
int storedMonth = prefs.getInt("myPreferenceKey", 0);
int currentMonth = LocalDate.now().getMonth().getValue();
if (storedMonth < currentMonth || (storedMonth == Month.DECEMBER.getValue() && currentMonth != Month.DECEMBER.getValue())) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("myPreferenceKey", currentMonth);
editor.apply();
}
Upvotes: 2