简介屏幕未打开

时间:2018-06-20 06:27:09

标签: android android-manifest

我们决定为我们的应用制作一个简介/欢迎屏幕。当用户首次进入应用程序时,需要启动名为“欢迎活动”的活动。所有其他时间都需要启动“主要活动”。这就是我在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。我有个班主任

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();
            }
        }
    });
}

2 个答案:

答案 0 :(得分:0)

尝试一下,它的工作原理...

我已经在Main Activity中创建了Shared Preference,并在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. 在Main Activity中,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>

答案 1 :(得分: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();
}
相关问题