SplashActivity 에서 (1) 인터넷 연결 검사 (2) 네트워크 통신 후 완료 되면 메인화면으로 이동
activity_splash.xml
<ImageView
android:id="@+id/icon"
android:transitionName="@string/iconTransition"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_launcher_background"
android:tint="@color/common_google_signin_btn_text_light"
android:src="@drawable/button"/>
<ImageView
android:id="@+id/loading"
android:layout_marginBottom="120dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="70dp"
android:layout_height="70dp" />
<LinearLayout
android:id="@+id/warningLayout"
android:orientation="vertical"
android:layout_marginBottom="120dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center"
android:src="@drawable/ic_warning"
android:layout_width="45dp"
android:layout_height="45dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/colorWhite"
android:text="모바일 데이터나 WiFi를 연결해주세요." />
</LinearLayout>
SplashActivity.java
public class LoadingActivity extends AppCompatActivity {
private LoadingViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
viewModel = ViewModelProviders.of(this).get(LoadingViewModel.class);
ImageView progressImageView = findViewById(R.id.loading);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(progressImageView);
Glide.with(this).load(R.raw.spinner).into(imageViewTarget);
View WarningMessageView = findViewById(R.id.warningLayout);
viewModel.isLoadedComplete().observe(this, result -> {
if (result == DataLoadState.SUCCESS) {
Intent intent = new Intent(LoadingActivity.this, MainActivity.class);
startActivity(intent, options.toBundle());
finish();
}
else if (result == DataLoadState.FAIL) {
//Error popup
}
});
viewModel.getNetworkConnectedLiveData().observe(this, connected -> {
if (connected) {
progressImageView.setVisibility(View.VISIBLE);
WarningMessageView.setVisibility(View.GONE);
}
else {
progressImageView.setVisibility(View.GONE);
WarningMessageView.setVisibility(View.VISIBLE);
}
});
}
}
public class LoadingViewModel extends AndroidViewModel {
private MutableLiveData<DataLoadState> loadedComplete = new MutableLiveData<>();
private MutableLiveData<Boolean> networkConnectedLiveData = new MutableLiveData<>();
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean connected = isNetworkConnected(context);
if (connected) {
context.unregisterReceiver(this);
}
networkConnectedLiveData.setValue(connected);
}
};
public LoadingViewModel(@NonNull Application application) {
super(application);
getDataOfNetwork(application);
loadedComplete.setValue(DataLoadState.NOT_YET);
networkConnectedLiveData.setValue(isNetworkConnected(application));
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
application.registerReceiver(receiver, intentFilter);
}
private boolean isNetworkConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
private void getDataOfNetwork(Context context) {
NetworkDataProvider
.getInstance()
.getData()
.observeOn(Schedulers.io())
.subscribe(it -> {
loadedComplete.postValue(DataLoadState.SUCCESS);
});
}
public LiveData<DataLoadState> isLoadedComplete() {
return loadedComplete;
}
public LiveData<Boolean> getNetworkConnectedLiveData() {
return networkConnectedLiveData;
}
}
public enum DataLoadState {
SUCCESS,
FAIL,
NOT_YET
}
def lifecycle_version = "2.0.0"
def futures_version = "1.0.0-alpha02"
implementation "androidx.concurrent:concurrent-futures:$futures_version"
implementation 'android.arch.lifecycle:extensions:1.0.0'
implementation "android.arch.lifecycle:common-java8:1.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'