android中的mvp模式:在splash頁面中,判斷是否有網絡連接,有則跳到下個頁面

hou 8年前發布 | 9K 次閱讀 Android開發 移動開發

來自: http://blog.csdn.net/knxw0001/article/details/39672917


1. 需求,這個是《android開發必知的50個訣竅》一書中的mvp章節的需求。
在splash頁面中,判斷是否有網絡連接,有則跳到下個頁面,無則彈出一條消息通知用戶,同時在檢查網絡是否正常的期間顯示一個進度條。


2. 類目錄結構



3. model接口和實現
public interface INetConnect {
     boolean isNetConnect( Context context);
}
public class NetConnect implements INetConnect {

 @Override
 public boolean isNetConnect(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                      .getSystemService(Context. CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                       .getActiveNetworkInfo();
             if (mNetworkInfo != null) {
                  return mNetworkInfo.isAvailable();
            }
       }
        return false;
 }

}

  1. view接口 public interface ISplashView { void showProcessBar(); void hideProcessBar(); void showNetError(); void startNextActivity(); }

  2. presenter實現 public class SplashPresenter { private INetConnect connect; private ISplashView iView;

    public SplashPresenter(ISplashView iView){

         this. iView = iView;
         connect = new NetConnect();
    

    }

    public void didFinishLoading(Context context){

         iView.showProcessBar();
         if( connect.isNetConnect(context)){
              iView.startNextActivity();
        } else{
              iView.showNetError();
        }
         iView.hideProcessBar();
    

    } }

6.activity中代碼 public class MainActivity extends Activity implements ISplashView{

 SplashPresenter presenter;
 private ProgressDialog progressBar;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout. activity_main);

        presenter = new SplashPresenter( this);
 }

 @Override
 protected void onResume() {
        super.onResume();
        presenter.didFinishLoading( this);
 }

 @Override
 public void showProcessBar() {
        if ( progressBar == null) {
             progressBar = new ProgressDialog( this);
             progressBar.setCancelable( true);
             progressBar.setCanceledOnTouchOutside( true);
             progressBar.setMessage( "更新數據中,請稍后" );
       }
        progressBar.show();
 }

 @Override
 public void hideProcessBar() {
        progressBar.hide();
 }

 @Override
 public void showNetError() {
       Toast. makeText(this, "暫無網絡", Toast.LENGTH_SHORT).show();
 }

 @Override
 public void startNextActivity() {
       Toast. makeText(this, "跳到下個activity", Toast.LENGTH_SHORT).show();
 }

}</pre>


7. 源碼鏈接

http://download.csdn.net/detail/knxw0001/7990065


 本文由用戶 hou 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!