檢查互聯網連接我已經創造了Android的一種形式,包括領域,如名字,姓氏,手機號碼等 我使用web服務來發送這個數據到服務器及其發生perfectly.Now我要檢查互聯網連接,如果當他填寫表單和用戶提交表單的用戶沒有連接到互聯網,形式應該得到上傳到服務器,每當用戶回來在網上connectivtiy。 任何人都可以告訴我實現此功能的方式。 我試着用廣播接收器來檢查互聯網連接,但沒有能夠得到形式上傳。我也必須將表單字段保存在永久存儲中。目前我將它們保存在bean類中。Android中
我貼我的完整形式的活動類的代碼。 讓我知道如果任何改變都需要
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean isValidEmail = false;
String first_name = firstname.getText().toString().trim();
String last_name = lastname.getText().toString().trim();
String country = nation.getText().toString().trim();
String mail = email.getText().toString().trim();
String firm = company.getText().toString().trim();
String mobile = phone.getText().toString().trim();
String post = title.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+";
if (first_name.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your first name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setFname(first_name);
if (last_name.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your last name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setLname(last_name);
if (firm.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your company name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setCompany(firm);
if (post.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your designation", Toast.LENGTH_LONG).show();
} else {
registrationBean.setTitle(post);
if (mobile.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your phone number", Toast.LENGTH_SHORT).show();
} else {
registrationBean.setPhone(mobile);
if (mail.equalsIgnoreCase("") && !mail.matches(emailPattern)) {
Toast.makeText(getApplicationContext(), "Invalid Email", Toast.LENGTH_LONG).show();
} else {
registrationBean.setMail(mail);
if (country.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your country of residence", Toast.LENGTH_SHORT).show();
} else {
registrationBean.setNation(country);
if (checked==true && registrationBean!=null)
{
}
else {
Toast.makeText(getApplicationContext(),"Please accept terms and conditions",Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
}
}
});
if (img_path.equalsIgnoreCase("")) {
retake_photo.setAlpha((float) 0.6);
}
}
private void selectImage() {
final CharSequence[] options = {"Take a Picture", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityForm.this);
builder.setCancelable(false);
builder.setTitle("Select Profile Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take a Picture")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
img_path = Environment.getExternalStorageDirectory() + "/temp.jpg";
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
if (options[item].equals("Choose from Gallery")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.addCategory(photoPickerIntent.CATEGORY_OPENABLE);
startActivityForResult(photoPickerIntent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
} else {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
startActivityForResult(intent, 2);
}
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
try {
bitmap = AppUtils.decodeFile(file.getPath(), 100, 100);
registrationBean.setImage(file.getPath());
// ExifInterface ei = new ExifInterface(file.getPath());
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
iv_profile.setImageBitmap(bitmap);
retake_photo.setAlpha(1);
}
break;
case 2:
try {
String uriImage;
imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageStream.close();
if (selectedImage != null) {
iv_profile.setImageBitmap(selectedImage);
retake_photo.setAlpha(1);
}
uriImage=imageUri.toString();
registrationBean.setImage(uriImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
private void callRegistrationService()
{
String fname=registrationBean.getFname();
String lname=registrationBean.getLname();
String company=registrationBean.getCompany();
String mobile=registrationBean.getPhone();
String designation=registrationBean.getTitle();
String image=getPath(imageUri);
String email=registrationBean.getMail();
String country=registrationBean.getNation();
//email,firstName,lastName,userImage,company,title,phone,country
new RegistrationAsyncTask(this, email, fname, lname, image, company, designation, mobile, country).execute();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Do something
callRegistrationService();
}
}
}
這種討厭'如果... ... else..if建設else'。你需要實現一種驗證器,它將以適當的方式進行驗證。或者,至少,你把它改爲'if(!check){Toast.makeText();返回; '序列。 –
這是表單字段的驗證器 –