0

我们决定为我们的应用制作一个介绍/欢迎屏幕。当用户第一次访问应用程序时,需要启动名为 Welcome Activity 的活动。所有其他时间主要活动都需要启动。这就是我在 Android Manifest 中的做法:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.gms.samples.vision.ocrreader"
android:installLocation="auto">

<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.CAMERA" />

<application
    android:name=".OcrApplication"
    android:allowBackup="true"
    android:fullBackupContent="false"
    android:hardwareAccelerated="true"
    android:icon="@drawable/icon"
    android:label="Ingredient analysis"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <meta-data
        android:name="com.google.android.gms.vision.DEPENDENCIES"
        android:value="ocr" />
    <activity android:name=".WelcomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="stateHidden|adjustPan"
        android:exported="true"
        >
    </activity>
    <activity
        android:name=".OcrCaptureActivity"
        android:label="Read Text" />
    <activity android:name=".ListResult" />
    <activity android:name=".AllIngredients" />
    <activity android:name=".IngredientDescription" />
    <activity android:name=".Instruction" />
</application>

是我的清单文件还是欢迎活动代码中的问题?我在 onCreate 中使用了 SharedPreferences。我有一个类 prefmanager

public class PrefManager {
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;

    // shared pref mode
    int PRIVATE_MODE = 0;

    // Shared preferences file name
    private static final String PREF_NAME = "androidhive-welcome";

    private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";

    public PrefManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void setFirstTimeLaunch(boolean isFirstTime) {
        editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
        editor.commit();
    }

    public boolean isFirstTimeLaunch() {
        return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
    }

}

创建时

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Checking for first time launch - before calling setContentView()
    prefManager = new PrefManager(this);
    if (!prefManager.isFirstTimeLaunch()) {
        launchHomeScreen();
        finish();
    }

    // Making notification bar transparent
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    setContentView(R.layout.activity_welcome);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
    btnSkip = (Button) findViewById(R.id.btn_skip);
    btnNext = (Button) findViewById(R.id.btn_next);


    // layouts of all welcome sliders
    // add few more layouts if you want
    layouts = new int[]{
            R.layout.welcome_slide1,
            R.layout.welcome_slide2,
            };

    // adding bottom dots
    addBottomDots(0);

    // making notification bar transparent
    changeStatusBarColor();

    myViewPagerAdapter = new MyViewPagerAdapter();
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

    btnSkip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchHomeScreen();
        }
    });

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // checking for last page
            // if last page home screen will be launched
            int current = getItem(+1);
            if (current < layouts.length) {
                // move to next screen
                viewPager.setCurrentItem(current);
            } else {
                launchHomeScreen();
            }
        }
    });
}
4

2 回答 2

0

试试这个,它的工作...

我在主活动中创建了共享首选项并在欢迎活动中检查了它。

如果它第一次运行,则没有找到任何共享偏好,并且会在欢迎活动中执行任何操作。

如果它运行第二次或更多次,它会找到 Shared Preference 然后重定向到 Main Activity 而不启动 Welcome Activity。

  1. 在欢迎活动中,onCreate 方法 -
if ((getSharedPreferences("Demo", MODE_PRIVATE).contains("value"))){
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
        finish();
    }
    
    else {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                finish();
            }
        },5000);
    }
  1. 在主活动中,onCreate 方法 -
SharedPreferences.Editor editor = getSharedPreferences("Demo",MODE_PRIVATE).edit();
    editor.putString("value", "1");
    editor.apply();
  1. 清单文件 -
<activity android:name=".WelcomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MainActivity"></activity>
于 2018-06-20T07:43:33.297 回答
0

你以前打过电话setFirstTimeLaunch(false)吗?如果不是,那是因为您true在函数中作为默认值返回:

public boolean isFirstTimeLaunch() {
    return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}

尝试将其更改为return pref.getBoolean(IS_FIRST_TIME_LAUNCH, false),不要忘记setFirstTimeLaunch(true)。我的建议是将其添加到此:

if (!prefManager.isFirstTimeLaunch()) {
    prefManager.setFirstTimeLaunch(true); // Add this
    launchHomeScreen();
    finish();
}
于 2018-06-20T10:51:46.100 回答