我希望在我的应用程序第一次在设备中打开时弹出一个对话框。我想在弹出框中显示如何使用该应用程序。 如果应用程序第一次打开它会显示对话框,否则只会避免显示对话框。活动也会根据第一次使用或正常使用而改变。首次使用时会显示一个activity1或者它会显示activity2.please帮助我。 这是我的活动,在应用程序打开时显示图像
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
// here i want to go to another activity acording to the first time use or normal time
}
}, 3000);
}
答案 0 :(得分:1)
public class class_name extends AppCompatActivity {
public static final String MyPREFERENCES2 = "MyPrefs" ;
SharedPreferences sharedpreferences2;
public boolean isFirstRun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
checkFirstRun();
}
}, 3000);
}
public void checkFirstRun() {
System.out.println("its in check first run");
isFirstRun = getSharedPreferences("PREFERENCE2", MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
startActivity(new Intent(class_name.this, new_activity1.class));
getSharedPreferences("PREFERENCE2", MODE_PRIVATE)
.edit()
.putBoolean("isFirstRun", false)
.commit();
}
else{
startActivity(new Intent(class_name.this, new_activity2.class));
}
}
}
答案 1 :(得分:0)
final String FIRST_TIME_KEY = "com.example.app.MainActivity.firstTimeKey";
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstTime = sp.getBoolean(FIRST_TIME_KEY, false);
if(isFirstTime) {
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean(FIRST_TIME_KEY, true);
edit.apply();
//show the dialog
}