【发布时间】:2018-06-28 01:54:10
【问题描述】:
我正在尝试构建一个 OnboardingActivity 并且我也已经实现了它,但现在我希望它应该对新用户可见或每个用户一次可见,我尝试了共享首选项,但没有效果。 我使用 Splash Screen 作为 Launcher Activity 和 MainActivty & onboardingActivity 作为下一个意图。
SplashActivity
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getSupportActionBar().hide();
Thread myThread = new Thread() {
@Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(), OnBoardActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
OnBoardActivity
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import com.hololo.tutorial.library.Step;
import com.hololo.tutorial.library.TutorialActivity;
public class OnBoardActivity extends TutorialActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("my_preferences",
MODE_PRIVATE);
// Check if onboarding_complete is false
if(!preferences.getBoolean("onboarding_complete",false)) {
// Start the onboarding Activity
Intent onboarding = new Intent(this, OnBoardActivity.class);
startActivity(onboarding);
// Close the main Activity
finish();
return;
}
//slide1
addFragment(new Step.Builder().setTitle("WELCOME.NAMASTE!!")
.setSummary("It seems like you are a New User!!\nWe welcome you
to GFeed.")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.gh) // int top drawable
.build());
//slide2
addFragment(new Step.Builder().setTitle("CAREER ORIENTED")
.setSummary("This app includes some awesome career oriented
elements such as Internship,different courses related to CS/IT,career
tips by experts.")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.human) // int top drawable
.build());
//slide3
addFragment(new Step.Builder().setTitle("COLLEGE ORIENTED")
.setSummary("This app includes some awesome college oriented
elements such as college club news,aktu feed,H.O.D'corner and Gsim")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.college) // int top drawable
.build());
}
@Override
public void finishTutorial() {
SharedPreferences preferences =
getSharedPreferences("my_preferences", MODE_PRIVATE);
// Set onboarding_complete to true
preferences.edit()
.putBoolean("onboarding_complete",true).apply();
// Launch the main Activity, called MainActivity
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
// Close the OnboardingActivity
finish();
}
}
清单文件
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/vtry"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait">
<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/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".developer"
android:screenOrientation="portrait">
</activity>
<activity android:name=".notify"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="NOTIFY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".download"
android:screenOrientation="portrait">
</activity>
<activity android:name=".webvr"
android:screenOrientation="portrait">
/>
<intent-filter>
<action android:name="WEBVR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".gsim"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Internship"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Courses"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Aktu"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Interview"
android:screenOrientation="portrait">
</activity>
<activity android:name=".Reachus"
android:screenOrientation="portrait">
</activity>
<activity android:name=".OnBoardActivity"
android:screenOrientation="portrait">
</activity>
<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/vtry" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>
【问题讨论】:
-
我看不出你在代码中哪里使用 SharedPreferences
-
实际上我已经删除了该代码,因为它使我的应用程序强制关闭。如果您能告诉我在哪里添加共享偏好代码以及该代码是什么会很好??
-
发布您的尝试,以便我们为您提供帮助
-
@JuanCruzSoler,我现在已经添加了共享偏好代码,你现在可以检查它
标签: java android android-layout android-studio-2.3