Good day friends of mine.
I want to add a functionality to my ANDROID application, which I think it is new to me... but it is new in the development world. The application runs very well at the moment... But I need to add this functionality as explained below.
Please see the sample picture below...which tells exactly what I want to do.
I want to check network connection state on Application Startup. If connection state is true, then, the application should load the MainActivity.xml. If connection state is false, then, the application should return a new String ("Error") message.
Please see my code sample below...in the onCreate() {} thread. NOTE: I am not proficient in android development please.
@Override
protected void onCreate(Bundle savedInstanceState) {
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();//this is line 91...
Log.d("Network Info", "" + activeNetwork);//log the connection info
if (activeNetwork != null && activeNetwork.isConnected()) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize the status bar textview control
statusBar = (TextView) findViewById(R.id.textViewStatusMsg);//status bar of the UI
titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of the UI
If the above condition is TRUE, the application runs well but if the following condition below is FALSE, the application crashes with ANDROID's error message "Unfortunately, App has crashed!". Instead of it giving me its own message and crashing, I want to kill the app programmatically using the else...statement below.
} else {
data = new String("NO NETWORK CONNECTION! TRY AGAIN...");
return; //return the message data and kill the APP's UI here
}
}
Any help will suffice please... I am not well vested in android programming and development...
I guess the reason for crashing is because of misplaced of super.onCreate method.
Move this statement outside if block and either you can do a toast message or show an alert dialog.
If above solution didn't work, comment the crash stacktrace.