关于SpalshActivity加载部分数据问题

pokerWu 发布于 2015/06/16 23:11
阅读 104
收藏 0
package com.example.girl;

import com.example.gilr.db.BaseDBManeger;
import com.example.gilr.db.DBImpl;
import com.example.girl.bean.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class SplashActivity extends Activity {
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 1){
				Intent intent = new Intent(SplashActivity.this,MainActivity.class);
				if(user != null){
					Bundle bu = new Bundle();
					bu.putSerializable("user", user);
					intent.putExtras(bu);
					load_text.setText(user.getName());
					Toast.makeText(SplashActivity.this, "欢迎回来"+user.getName(), Toast.LENGTH_SHORT).show();
				}
				startActivity(intent);
				finish();
			}
		}
		
	};
	private TextView load_text;
	private User user =null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		setContentView(R.layout.splash_activity_layout);
		
		load_text = (TextView) findViewById(R.id.load_text);
		new Thread(){

			@Override
			public void run() {
				Log.i("poker-------------->","db begin");
				try {
					/**
					 * 创建已一条消息
					 */
					Message msg = new Message();
					msg.what =1;
					/**
					 * 查询开始时间
					 */
					long startTime = System.currentTimeMillis();
					/**
					 * 查询用户是否存在
					 */
					BaseDBManeger maneger = new DBImpl(SplashActivity.this);
					user = maneger.findUserByName();
					/**
					 * 结束查询时间
					 */
					long endTime = System.currentTimeMillis();
					Log.i("poker-------------->","2");
					/**
					 * 为了保证用户的体验,如果太快查询出来,开始界面会一闪就没了,不好
					 */
					
					if(endTime-startTime < 3*100){
						sleep(3000-(endTime-startTime));
						//handler.sendMessageDelayed(msg, 3000-(endTime-startTime));
					}
					handler.sendMessage(msg);
				} catch (Exception e) {
					
					e.printStackTrace();
				}
			}
			
		}.start();
		
	}

}

这是根据自己的理解写的一个另开线程加载数据,做法有何不妥。为何我使线程休眠能成功,发送延迟的消息测会出现黑屏。求大神假打...



加载中
0
C
ChenGuanwei
建议把new thread(){}.start()放到onResume里做
0
linwoain
linwoain
你黑屏是不是手机的锁屏时间到了,我测试了你的代码,没发现问题
pokerWu
pokerWu
当我handler发送延时消息会出现黑屏,thread.sleep()就没有这个情况
返回顶部
顶部