Android引导页实现后启动页没了,该怎么处理

Android引导页实现后启动页没了
本帖最后由 u014294325 于 2015-06-04 10:22:21 编辑
SplashActivity.java



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ProgressDialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.content.Context;

import java.io.File;

import android.support.v4.view.ViewPager;

import android.os.Environment;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import android.net.Uri;
import android.view.View;
import android.widget.TextView;

/**
 *
 * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
 *
 *     class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 *     (2)是,则进入GuideActivity;否,则进入MainActivity (3)3s后执行(2)操作
 *
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
 *
 *
 */
public class SplashActivity extends Activity {
    private final int SPLASH_DISPLAY_LENGHT = 3000; // 延迟3秒
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private final int UPDATA_NONEED = 0;
    private final int UPDATA_CLIENT = 1;
    private final int GET_UNDATAINFO_ERROR = 2;
    private final int SDCARD_NOMOUNTED = 3;
    private final int DOWN_ERROR = 4;
    private static final long SPLASH_DELAY_MILLIS = 3000;
    boolean isFirstIn = false;
    private static final String SHAREDPREFERENCES_NAME = "first_pref";




    /**
     * Handler:跳转到不同界面
     */
    private Handler mHandler = new Handler() {


        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case UPDATA_CLIENT:
                    //对话框通知用户升级程序
                    downLoadApk();
                    break;
                case DOWN_ERROR:
                    //下载apk失败
                    Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_SHORT).show();
                    break;
//                case GO_GUIDE:
//                    goGuide();
//                    break;
            }

            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

//        new Handler().postDelayed(new Runnable(){
//
//            @Override


        init();


        //检查网络
        if(chkNetwork()){
            CheckAppVersion();
        }
//        else {
//            mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
//        }

    }


    private void init() {
        // 读取SharedPreferences中需要的数据
        // 使用SharedPreferences来记录程序的使用次数
        //  SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);

        // 取得相应的值,如果没有该值,说明还未写入,用true作为默认值

        SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        isFirstIn = preferences.getBoolean("isFirstIn", true);
        if (!isFirstIn) {
            Intent intent = new Intent(SplashActivity.this, MyActivity.class);
            SplashActivity.this.startActivity(intent);
            SplashActivity.this.finish();
        } else {
            Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
            SplashActivity.this.startActivity(intent);
            SplashActivity.this.finish();
        }
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("isFirstIn", false);
        // 提交修改
        editor.commit();
        // 判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面
//        if (!isFirstIn) {
//            // 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
//            mHandler.sendEmptyMessageDelayed(GO_HOME, 5000);
//        } else {
//            mHandler.sendEmptyMessageDelayed(GO_GUIDE, 3000);
//        }


    }


    //检查网络
    public boolean chkNetwork(){
        try{
            ConnectivityManager connectivityManager = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if(networkInfo != null && networkInfo.isAvailable()){
                return true;
            }else{
                return false;
            }
        }catch(Exception e){
            return false;
        }
    }



    public void CheckAppVersion(){
        try{
            getLocalVerInfo();
            Thread_getUpdVerInfo chkVer = new Thread_getUpdVerInfo();
            chkVer.start();
            chkVer.join();
            checkVersion();
        }catch(Exception e){
            //tvMsg.setText(e.getMessage());
        }
    }

    public void getLocalVerInfo(){
        try{
            UpdateGlobal.localVerCode = this.getPackageManager().getPackageInfo(this.getPackageName(),0).versionCode;
            UpdateGlobal.localVerName = this.getPackageManager().getPackageInfo(this.getPackageName(),0).versionName;
        }catch(Exception e){
            //tvMsg.setText(e.getMessage());
        }
    }

    /**
     * 检查更新版本
     */
    public void checkVersion(){
        //Toast.makeText(getApplicationContext(), UpdateGlobal.localVerCode+";"+ UpdateGlobal.serverVerCode, Toast.LENGTH_SHORT).show();

        TextView tvVersionsShow = (TextView)findViewById(R.id.tvVersionsShow);
        tvVersionsShow.append(""+UpdateGlobal.localVerName);

        if(UpdateGlobal.localVerCode < UpdateGlobal.serverVerCode){
            //发现新版本,提示用户更新
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("软件升级")
                    .setMessage("发现新版本 "+UpdateGlobal.serverVerName+",建议立即更新使用.")
                    .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //开启更新服务UpdateService
                            //这里为了把update更好模块化,可以传一些updateService依赖的值
                            //如布局ID,资源ID,动态获取的标题,这里以app_name为例
                            //Intent updateIntent =new Intent(SplashActivity.this, UpdateService.class);
                            //updateIntent.putExtra("titleId",R.string.app_name);
                            //startService(updateIntent);
                            Message msg = new Message();
                            msg.what = UPDATA_CLIENT;
                            mHandler.sendMessage(msg);
                        }
                    })
                    .setNegativeButton("取消",new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
                        }
                    });
            alert.create().show();
        }else{
            //清理工作,略去
            //cheanUpdateFile(),文章后面我会附上代码
            mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
        }
    }




    private void goHome() {
        Intent intent = new Intent(SplashActivity.this, MyActivity.class);
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
    }

    private void goGuide() {
        Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
    }







    //从服务器中下载APK
    protected void downLoadApk() {
        final ProgressDialog pd;    //进度条对话框
        pd = new  ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载更新");
        pd.show();
        new Thread(){
            @Override
            public void run() {
                try {
                    File file = DownLoadManager.getFileFromServer(UpdateGlobal.serverDownUrl, pd);
                    sleep(3000);
                    installApk(file);
                    pd.dismiss(); //结束掉进度条对话框
                } catch (Exception e) {
                    Message msg = new Message();
                    msg.what = DOWN_ERROR;
                    mHandler.sendMessage(msg);
                    e.printStackTrace();
                }
            }}.start();
    }

    //安装apk
    protected void installApk(File file) {
        Intent intent = new Intent();
        //执行动作
        intent.setAction(Intent.ACTION_VIEW);
        //执行的数据类型
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        startActivity(intent);
    }

    //根据传过来url创建文件
    private File getFile(String url) {
        File files = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), getFilePath(url));
        return files;
    }
    //截取出url后面的apk的文件名
    private String getFilePath(String url) {
        return url.substring(url.lastIndexOf("/"), url.length());
    }

    /**
     * Created by Amy on 2015/6/3 0003.
     */


}


------解决思路----------------------
先用handler.postDelay 让启动页休眠3秒,然后再判断是不是第一次进入即可!
你的休眠写在判断的时候,是不科学的。
------解决思路----------------------
参照下面的代码即可。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent;
//如果第一次,则进入引导页WelcomeActivity
if (first) {
intent = new Intent(MainActivity.this,
WelcomeActivity.class);
} else {
intent = new Intent(MainActivity.this,
HomeActivity.class);
}
startActivity(intent);
// 设置Activity的切换效果
overridePendingTransition(R.anim.in_from_right,
R.anim.out_to_left);
MainActivity.this.finish();
}
}, TIME);
------解决思路----------------------
参照下面的代码即可。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent;
//如果第一次,则进入引导页WelcomeActivity
if (first) {
intent = new Intent(MainActivity.this,
WelcomeActivity.class);
} else {
intent = new Intent(MainActivity.this,
HomeActivity.class);
}
startActivity(intent);
// 设置Activity的切换效果
overridePendingTransition(R.anim.in_from_right,
R.anim.out_to_left);
MainActivity.this.finish();
}
}, TIME);
------解决思路----------------------
可以用sharedpreferences保存一条数据,那条数据标志着是否是第一次登陆。然后每次执行启动页的时候就去取出那条数据,如果那条数据代表第一登陆就跳转到引导页如果不是就跳到首页。