一个基本的启动画面
启动画面就像任何其他活动一样,但它可以在后台处理你的所有启动需求。例:
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
现在我们的启动画面将被称为第一个活动。
这是一个示例启动画面,它还处理一些关键应用程序元素:
public class Splash extends Activity{
public final int SPLASH_DISPLAY_LENGTH = 3000;
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WAKE_LOCK,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE},
123);
}
}
@Override
protected void onCreate(Bundle sis){
super.onCreate(sis);
//set the content view. The XML file can contain nothing but an image, such as a logo or the app icon
setContentView(R.layout.splash);
//we want to display the splash screen for a few seconds before it automatically
//disappears and loads the game. So we create a thread:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//request permissions. NOTE: Copying this and the manifest will cause the app to crash as the permissions requested aren't defined in the manifest.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
checkPermission();
}
String lang = [load or determine the system language and set to default if it isn't available.]
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration ();
config.locale = locale;
Splash.this.getResources().updateConfiguration(config,
Splash.this.getResources().getDisplayMetrics()) ;
//after three seconds, it will execute all of this code.
//as such, we then want to redirect to the master-activity
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
//then we finish this class. Dispose of it as it is longer needed
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
public void onPause(){
super.onPause();
finish();
}
}