gpt4 book ai didi

java - 如何在一天中每 5 次启动时显示对话框?

转载 作者:太空狗 更新时间:2023-10-29 13:55:13 26 4
gpt4 key购买 nike

这是我的代码,它正在运行请告诉我我应该为每 5 次启动对话框编写什么代码,这意味着当用户在一天中第 5 次启动应用程序然后应该显示用于评价应用程序的对话框,类似地每 5 次应显示启动对话框。

 public class MainActivity extends Activity {


String android_id,version,ver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


//********************For Rating APP **********************
SharedPreferences sharedPrefs = MainActivity.this.getSharedPreferences("RATER", 0);

SharedPreferences.Editor prefsEditor = sharedPrefs.edit();

long time = sharedPrefs.getLong("displayedTime", 0);
if (time < System.currentTimeMillis() - 259200000) {
displayDialog();
prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
}
}

//dialog box Function for rating app.

private void displayDialog() {
// TODO Auto-generated method stub
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
Intent in = new Intent(android.content.Intent.ACTION_VIEW);
in.setData(Uri.parse(url));
startActivity(in);
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Rate This App");
builder.setMessage("You really seem to like this app, "
+"since you have already used it %totalLaunchCount% times! "
+"It would be great if you took a moment to rate it.")
.setPositiveButton("Rate Now", dialogClickListener)
.setNegativeButton("Latter", dialogClickListener)
.setNeutralButton("No,thanks", dialogClickListener).show();

}
//End dialog box Function for rating app.
}

这是我的代码,实际上我想在应用程序中实现应用程序评级对话框,它应该在一天中每 5 次启动时显示一次

最佳答案

使用以下方法检查启动次数。

private void checkLaunchCount()
{
int sessionUniqueId = mSharedPref.getInt("DATE_UNIQUE", 0);
int launchCount = mSharedPref.getInt("LAUNCH_COUNT", 0);
if(sessionUniqueId != 0)
{
int todayUniqueId = getUniqueNumberFromDate();
if(todayUniqueId == sessionUniqueId)
{
if(launchCount >= 5)
{
//Show Dialog
}
else
{
updateLaunchCount(launchCount);
}
}
else
{
updateUniqueDateId();
updateLaunchCount(launchCount);
}

}
else
{
updateUniqueDateId()
updateLaunchCount(launchCount);
}
}

此方法用于获取 Date 的 UniqueInt

private int getUniqueNumberFromDate()
{
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
}

此方法用于在 SharedPref 中节省时间

private static SharedPreferences mSharedPref;
private void updateUniqueDateId()
{
if(mSharedPref == null)
mSharedPref = getActivity().getSharedPreferences(getActivity().getPackageName(), Activity.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putInt("DATE_UNIQUE", getUniqueNumberFromDate()).commit();
}

此方法用于在 SharedPref 中保存计数

 private void updateLaunchCount(int launchCount)
{
launchCount++;
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putInt("LAUNCH_COUNT", launchCount).commit();
}

关于java - 如何在一天中每 5 次启动时显示对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com