package studio_codec.reportingsystem;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import javax.xml.datatype.Duration;
public class MainActivity extends Activity {
public EditText username = (EditText) findViewById(R.id.username);
public EditText password = (EditText) findViewById(R.id.password);
public String Username = username.getText().toString();
public String Password = password.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onStart() {
if (!internet_check()) {
Context context = getApplication();
int duration = Toast.LENGTH_SHORT;
CharSequence Error_connection_message = "Please Connect with Internet.Turn on Mobile Data Service";
Toast Error_Connection = Toast.makeText(context, Error_connection_message, duration);
Error_Connection.show();;
}
}
protected void onResume() {
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
if (Username == null && (Password.equals(""))) {
Context context = getApplicationContext();
CharSequence text = "Please Check your Login Credentials! ";
int duration = Toast.LENGTH_SHORT;
Toast Error_credentials = Toast.makeText(context, text, duration);
Error_credentials.show();
}
else{
// here ..use a function to parse json data and connect to server
}
}
});
}
public boolean internet_check() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
}
我刚刚阅读了Android Activity Life Cycle,我想知道我编写的代码将按照我的计划运行。我需要代码在用户点击我的应用程序时显示XML文件,一旦在onStart方法中我需要知道设备是否连接到网络,当我需要onResume时,如果用户点击按钮,我需要检查字段是否为空。这里我只有两个文本字段。一个用于用户名,另一个用于密码。 谢谢。