版权声明:本文为Yiwent原创文章,转载必须注明出处。
做一个APP,首先是它的首页,给人一个品牌的形象,让人一眼就知道你的app做什么的。
先是主要代码:
1.activity主要代码
public class SplashActivity extends AppCompatActivity {
private boolean isNeedSetting;
private boolean isNeedLogin = true;
private boolean isFirstRun = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
initData();
initConnect();
// initGPS();
}
2.XML代码:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:background="@mipmap/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3.initData()方法
private void initData() {
isFirstRun = getSharedPreferences(MyConstains.SP_MOBIKE, MODE_PRIVATE).
getBoolean(IS_FIRST_RUN, true);
isNeedLogin = getSharedPreferences(MyConstains.SP_MOBIKE, MODE_PRIVATE).
getBoolean(MyConstains.IS_NEED_LOGIN, true);
}
4.initConnect()方法
private void initConnect() {
if (!isConn(this)) {
//提示对话框
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
builder.setMessage("网络无法访问,请检查网络").setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
//判断手机系统的版本 即API大于10 就是3.0或以上版本
if (android.os.Build.VERSION.SDK_INT > 21) {
intent = new Intent(Settings.ACTION_HOME_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
startActivityForResult(intent, 1);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
5.判断网络是否正常
/**
* 判断网络连接是否已开
* true 已打开 false 未打开
*/
public static boolean isConn(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
6.判断GPS是否正常
private void initGPS() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// 判断GPS模块是否开启,如果没有则跳转至设置开启界面,设置完毕后返回到首页
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
builder.setTitle("GPS提示:");
builder.setMessage("请打开GPS开关,以便您更准确的找到自行车");
builder.setCancelable(false);
builder.setPositiveButton("确定",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// 转到手机设置界面,用户设置GPS
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 2); // 设置完成后返回到原来的界面
}
builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
7.跳转设置后重新判断
@Override
protected void onResume() {
super.onResume();
if (isNeedSetting) {
if (isConn(this)) {
checkFirstRun();
} else {
initConnect();
}
8.判断是否第一次运行,如果是,跳转引导界面
private void checkFirstRun() {
if (isFirstRun) {
Go2Guide();
getSharedPreferences(MyConstains.SP_MOBIKE, MODE_PRIVATE)
.edit()
.putBoolean(MyConstains.IS_FIRST_RUN, false)
.apply();
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (isNeedLogin) {
Go2Login();
} else {
Go2Main();
}
9.activity间的跳转
private void Go2Login() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void Go2Main() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
private void Go2Guide() {
Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
startActivity(intent);
finish();
}
10.最后是跳转设置界面返回判断
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
case 2:
isNeedSetting = true;
break;
default:
break;
}
首界面代码十分简单,主要判断网络,因为使用该APP必须联网的,最好是开GPS定位的,其次是延迟2S判断进入哪个页面。
第一次写博客,喜欢希望多给个start,老铁,抱拳了。