我有一个简单的对话框,如果有.apk
的新版本,应该显示该对话框。问题在于,每次调用onResume()
方法时它都会显示。
我想知道是否每月只能显示一次此对话框。
下面是Dialog
的代码以及告诉我是否有新.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);
}
}
答案 0 :(得分:1)
上一个答案的主意正确,但是执行不正确,因为该答案在一年中的最后一个月之后不起作用!
您可以使用以下方法测试是否已过去一个月(30天):
public boolean hasMonthPassed() {
long lastTimestamp = prefs.getLong("myPreferenceKey", 0);
long currentTimestamp = System.currentTimeMillis();
return (currentTimestamp - lastTimestamp) >= TimeUnit.DAYS.toMillis(30);
}
然后您可以在班级的其他地方使用它:
if(hasMonthPassed()) {
// Your code here
// Set the preference
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("myPreferenceKey", System.currentTimeMillis());
editor.apply()
}
或者,可以通过更新以下内容来修复先前的答案:
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();
}