android loading界面 及 处理


主要实现效果:
--------------
程序启动,进入loading界面,
loading界面显示背景图 及 进度条动画,
后台启动线程进行相应的初始化操作,
loading界面更新相应的初始化提示信息,
初始化完成,打开并进入主界面,关闭loading界面,
如果初始化超时,则弹出提示,退出程序。

 

loading.java
------------

package com.hello;

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

public  class loading  extends Activity {

int MSG_INIT_OK = 1;
int MSG_INIT_INFO = 2;
int MSG_INIT_TIMEOUT = 9;
 
boolean isTimeout =  false;
 
TextView tvInfo ;
 
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.loading);
        tvInfo = (TextView)findViewById(R.id.tvInfo);
        initSystem();
            
    }
    
     private  void initSystem(){
     initThread();
     mHandler.postDelayed(timeOutTask,60000); // 60秒超时
    }
    
    
     public Handler mHandler =  new Handler(){     
      public  void handleMessage(Message msg){
       if(msg.what == MSG_INIT_TIMEOUT){      
 
        if (mHandler !=  null && timeOutTask !=  null ){
        mHandler.removeCallbacks(timeOutTask);
       }
       
       Toast.makeText(loading. this, "timeout", Toast.LENGTH_LONG).show();
       loading. this.finish();
       
      } else  if(msg.what == MSG_INIT_OK){
       
        if (mHandler !=  null && timeOutTask !=  null ){
        mHandler.removeCallbacks(timeOutTask);
       }
       
       startActivity( new Intent(getApplication(),MainActivity. class));
                loading. this.finish();
      } else  if(msg.what == MSG_INIT_INFO){

       String info = msg.obj.toString();
       tvInfo.setText(info);       
      }
     }     
    };
    
    Runnable timeOutTask =  new Runnable() {
   public  void run() {
   
   isTimeout =  true;
   
   Message msg = Message.obtain();
   msg.what = MSG_INIT_TIMEOUT;
   mHandler.sendMessage(msg);
  }    
    };
    
     private  void initThread(){
      new Thread(){
       public  void run() {
        try {
         if(!isTimeout){
         sendInitInfo("初始化 step 1");         
         Thread.sleep(1000); // TODO 1
        }
        
         if(!isTimeout){
         sendInitInfo("初始化 step 2");
         Thread.sleep(2000); // TODO 2        
        }
        
         if(!isTimeout){
         sendInitInfo("初始化 step 3");
         Thread.sleep(3000); // TODO 2
        }
     
      if(!isTimeout){
       // 初始化完成
      sendInitInfo("初始化完成");
         
      Message msg2 = Message.obtain();
      msg2.what = MSG_INIT_OK;
      mHandler.sendMessage(msg2);
     }
     
    }  catch (InterruptedException e) {
      //  TODO Auto-generated catch block
     e.printStackTrace();
    }
      }           
     }.start();
    }

     private  void sendInitInfo(String info){
     Message msg1 = Message.obtain();
  msg1.what = MSG_INIT_INFO;
  msg1.obj = info;
  mHandler.sendMessage(msg1);
    }
}


 res/layout/loading.xml
----------------------

<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
 android:layout_width
="match_parent"  android:layout_height ="match_parent"
 android:background
="@drawable/pf_loading_bg"  android:orientation ="horizontal" >

  < TableLayout  android:layout_width ="match_parent"
  android:layout_height
="match_parent" >
   < TableRow >
    < ProgressBar  android:id ="@+id/progressBar1"
    android:layout_width
="fill_parent"  android:layout_height ="wrap_content"
    android:paddingTop
="10dip"  android:paddingLeft ="70dip"
    android:paddingRight
="70dip"  style ="@style/loading" >
    </ ProgressBar >
   </ TableRow >
   < TableRow >
    < TextView  android:text ="TextView"  android:layout_height ="wrap_content"
    android:id
="@+id/tvInfo"  android:layout_width ="wrap_content" ></ TextView >
   </ TableRow >
  </ TableLayout >
 
</ LinearLayout >

 

res/values/mystyle.xml
----------------------

<? xml version="1.0" encoding="utf-8" ?>
< resources >
     < style  name ="loading"  parent ="@android:style/Widget.ProgressBar.Large" >
         < item  name ="android:indeterminateDrawable" >@anim/loading </ item >
     </ style >
</ resources >


res/anim/loading.xml
--------------------

<? xml version="1.0" encoding="utf-8" ?>
< animation-list  android:oneshot ="false"  
xmlns:android
="http://schemas.android.com/apk/res/android"   >
< item  android:duration ="100"  android:drawable ="@drawable/loading1"   />
< item  android:duration ="100"  android:drawable ="@drawable/loading2"   />
< item  android:duration ="100"  android:drawable ="@drawable/loading3"   />
</ animation-list >

 

res/drawable-hdpi/
-------------------
loading_bg.png ,
loading1.png , loading2.png ,loading3.png


AndroidMainfest.xml
-------------------

<? xml version="1.0" encoding="utf-8" ?>
< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
      package
="com.hello"
      android:versionCode
="1"
      android:versionName
="1.0" >

     < application  android:icon ="@drawable/icon"  android:label ="@string/app_name" >
         < activity  android:name =".MainActivity"
                  android:label
="@string/app_name" >
         </ activity >
        
         < activity  android:name =".loading" >
             < intent-filter >
                 < action  android:name ="android.intent.action.MAIN"   />
                 < category  android:name ="android.intent.category.LAUNCHER"   />
             </ intent-filter >
         </ activity >

     </ application >
</ manifest >


 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM