我不知道这个问题是否被提出,但我找不到。 我希望警报对话框显示每10次或更多次启动应用程序。
AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
a_builder.setMessage("Please take time and rate our application")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));
}
}
}).setNegativeButton("Not Now",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}) ;
AlertDialog alert = a_builder.create();
alert.setTitle("Rate Us !");
alert.show();
答案 0 :(得分:5)
设置sharedPreferences并每次按存储在其中的值递增计数。当值达到计数时,只需显示警报并重置共享偏好。
答案 1 :(得分:1)
int count=0;
int prefcount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit=pref.edit();
edit.putInt("Count",count);
edit.commit();
prefcount=pref.getInt("Count",-1);
if(prefcount>10){
//show dialog
}
}
}
Hope this will help you.
答案 2 :(得分:1)
您可以在共享首选项中存储一个整数值,以计算您的应用启动次数
您可以在启动活动的OnCreate()方法或作为应用程序入口点的任何其他活动中增加值(例如来自通知)。
您应该在每次显示对话框后重置该值
这是一小段代码 -
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
int appLaunchCount = pref.getInt("appLaunchCount",-1);
if(appLaunchCount==10){
// code to show dialog
// reset
appLaunchCount=0;
} else {
// increment count
appLaunchCount = appLaunchCount+1;
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("appLaunchCount", appLaunchCount);
editor.apply();
答案 3 :(得分:0)
您可以在SharedPreferences中存储开放计数
在oncreate活动上添加计数器。
SharedPreferences prefs = getSharedPreferences("user_count", MODE_PRIVATE);
SharedPreferences.Editor editor1 = getSharedPreferences("user_count", MODE_PRIVATE).edit();
int restoredCount = prefs.getInt("name", 0);
if (restoredCount == 10) {
editor1.putInt("name", 0);
editor1.commit();
// Here Show Alert Dialog.
}
//获取计数器值
UPDATE `wp_buddypress_dev`.`wp_usermeta`
SET `meta_value` = REPLACE(`meta_value`, 'a:1:{s:10:"subscriber";b:1;}')
WHERE `meta_value` = 'a:1:{s:10:"a:1:{s:10:"subscriber";b:1;}";';
我希望这个例子可以帮到你。