APP啟動(dòng)時(shí)經(jīng)常會(huì)有一張啟動(dòng)圖片,有幾秒鐘的展示時(shí)間充易,做法很簡(jiǎn)單菇民,使用Handler的postDelayed方法即可。
- 配置全屏:在styles.xml中定義Theme持钉,代碼如下:
<!--設(shè)置-全屏-->
<style name="NoTitleFullscreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
- 在AndroidManifest.xml文件中使用這個(gè)Theme
android:theme="@style/NoTitleFullscreen"
- 配置啟動(dòng)延遲代碼如下:
private void delayedStart(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(IntentUtil.getLauncherIntent().setClass(getApplicationContext(), MainActivity.class));
finish();
}
}, 3000);
}
- 調(diào)用方式:在Activity的onCreate方法中調(diào)用即可:
delayedStart();
- 這里有一個(gè)Intent的工具類IntentUtil.java衡招,作用是用于啟動(dòng)Activity并可以傳遞參數(shù)。IntentUtil.java代碼如下:
package cn.studyou.parchment.utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* 基本功能:Intent工具類
* 創(chuàng)建:王杰
* 創(chuàng)建時(shí)間:16/3/7
* 郵箱:w489657152@gmail.com
*/
public class IntentUtil {
private static final String TAG = IntentUtil.class.getSimpleName();
public static Intent getLauncherIntent() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
return intent;
}
public static void logIntent(String tag, Intent intent) {
if (intent == null) {
return;
}
StringBuffer sb = new StringBuffer();
sb.append("\nAction:" + intent.getAction());
sb.append("\nData:" + intent.getData());
sb.append("\nDataStr:" + intent.getDataString());
sb.append("\nScheme:" + intent.getScheme());
sb.append("\nType:" + intent.getType());
Bundle extras = intent.getExtras();
if (extras != null && !extras.isEmpty()) {
for (String key : extras.keySet()) {
Object value = extras.get(key);
sb.append("\nEXTRA: {" + key + "::" + value + "}");
}
} else {
sb.append("\nNO EXTRAS");
}
Log.i(tag, sb.toString());
}
public static int sdkVersion() {
return new Integer(Build.VERSION.SDK).intValue();
}
public static void startDialer(Context context, String phoneNumber) {
try {
Intent dial = new Intent();
dial.setAction(Intent.ACTION_DIAL);
dial.setData(Uri.parse("tel:" + phoneNumber));
context.startActivity(dial);
} catch (Exception ex) {
Log.e(TAG, "Error starting phone dialer intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app to place a phone call!",
Toast.LENGTH_SHORT).show();
}
}
public static void startSmsIntent(Context context, String phoneNumber) {
try {
Uri uri = Uri.parse("sms:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra("address", phoneNumber);
intent.setType("vnd.android-dir/mms-sms");
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting sms intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app to send an SMS!",
Toast.LENGTH_SHORT).show();
}
}
public static void startEmailIntent(Context context, String emailAddress) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,
new String[]{emailAddress});
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting email intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app for sending emails!",
Toast.LENGTH_SHORT).show();
}
}
public static void startWebIntent(Context context, String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(intent);
} catch (Exception ex) {
Log.e(TAG, "Error starting url intent.", ex);
Toast.makeText(context,
"Sorry, we couldn't find any app for viewing this url!",
Toast.LENGTH_SHORT).show();
}
}
}
- 使用方式:
startActivity(IntentUtil.getLauncherIntent().setClass(getApplicationContext(), MainActivity.class));
startActivity(IntentUtil.getLauncherIntent().setClass(getApplicationContext(), MainActivity.class).putExtra("name","name"));