1

I want my application(android) to go to another activity on the first launch, so people immediately change their settings. After I update my application I want to show a changelog. I know you can make this happen with shared preferences and I made something like this:

package me.wouter.schoolwork;

import android.app.Activity;
import android.app.AlertDialog;

import android.os.Bundle;


public class Updater extends Activity{


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

public void checkFirstLaunch(){
    boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
    if (firstrun){
        new AlertDialog.Builder(this).setTitle("Welcome").setMessage("Welcome on  ").setNeutralButton("OK", null).show();

    getSharedPreferences("PREFERENCE", MODE_PRIVATE)
        .edit()
        .putBoolean("firstrun", false)
        .commit();
    }
}

}

I'm not sure if this works, and this doesn't work for updating. Can anyone help me?

Thanks in advance :)

4

1 回答 1

3

尝试这个:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int currentVersion = 0;
    try {
        currentVersion = getPackageManager().getPackageInfo(getSherlockActivity().getPackageName(), PackageManager.GET_CONFIGURATIONS).versionCode;
    } catch (final NameNotFoundException e) {
    }

    final int lastVersion = prefs.getInt("lastVersion", -1);
    if (currentVersion > lastVersion) {
        showChangelogDialog();
        prefs.edit().putInt("lastVersion", currentVersion).commit();
    }
于 2012-12-30T11:01:49.250 回答