介绍屏幕未打开
Intro screen is not opening
我们决定为我们的应用制作一个 intro/welcome 屏幕。当用户第一次进入应用程序时,需要启动名为 Welcome Activity 的 activity。所有其他时间 Main Activity 都需要启动。这就是我在 Android 清单中的做法:
<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>
这是我的清单文件中的问题还是欢迎 Activity 代码中的问题?我在 onCreate 中使用了 SharedPreferences。我有一个 class 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();
}
}
});
}
试试这个,它的工作...
我在 Main Activity 中创建了 Shared Preference 并在 Welcome Activity 中检查了它。
如果是 运行 第一次,则未找到任何共享首选项,将在欢迎时执行任何操作 Activity。
如果它 运行 次或更多次,它会找到共享首选项然后重定向到 Main Activity 而不启动 Welcome Activity.
- 欢迎使用 Activity,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);
}
- 在 Main Activity 中,onCreate 方法 -
SharedPreferences.Editor editor = getSharedPreferences("Demo",MODE_PRIVATE).edit();
editor.putString("value", "1");
editor.apply();
- 清单文件 -
<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>
你以前给setFirstTimeLaunch(false)
打过电话吗?如果不是,那是因为你 return 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();
}
我们决定为我们的应用制作一个 intro/welcome 屏幕。当用户第一次进入应用程序时,需要启动名为 Welcome Activity 的 activity。所有其他时间 Main Activity 都需要启动。这就是我在 Android 清单中的做法:
<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>
这是我的清单文件中的问题还是欢迎 Activity 代码中的问题?我在 onCreate 中使用了 SharedPreferences。我有一个 class 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();
}
}
});
}
试试这个,它的工作...
我在 Main Activity 中创建了 Shared Preference 并在 Welcome Activity 中检查了它。
如果是 运行 第一次,则未找到任何共享首选项,将在欢迎时执行任何操作 Activity。
如果它 运行 次或更多次,它会找到共享首选项然后重定向到 Main Activity 而不启动 Welcome Activity.
- 欢迎使用 Activity,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); }
- 在 Main Activity 中,onCreate 方法 -
SharedPreferences.Editor editor = getSharedPreferences("Demo",MODE_PRIVATE).edit(); editor.putString("value", "1"); editor.apply();
- 清单文件 -
<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>
你以前给setFirstTimeLaunch(false)
打过电话吗?如果不是,那是因为你 return 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();
}