android中的mvp模式:在splash页面中,判断是否有网络连接,有则跳到下个页面

hou 6年前

来自: 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;       }    }    4. view接口  public interface ISplashView {       void showProcessBar();       void hideProcessBar();       void showNetError();       void startNextActivity();  }    5. 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();       }    }


7. 源码链接

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