Java tutorial
package chinanurse.cn.nurse.Fragment_Nurse_job; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.baidu.mobstat.StatService; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import chinanurse.cn.nurse.HttpConn.HttpConnect; import chinanurse.cn.nurse.HttpConn.request.StudyRequest; import chinanurse.cn.nurse.R; import chinanurse.cn.nurse.UrlPath.NetBaseConstant; import chinanurse.cn.nurse.utils.RegexUtil; import chinanurse.cn.nurse.WebView.News_WebView_url; import chinanurse.cn.nurse.bean.News_list_type; import chinanurse.cn.nurse.bean.UserBean; import chinanurse.cn.nurse.bean.mine_main_bean.Company_news_beans; public class IdentityFragment_ACTIVITY extends Activity implements View.OnClickListener { private static final int COMPANYCERTIFY = 7; private static final int RESULT_CANCELED = 0; private static final int GETCOMANYCERTIFY = 8; private EditText et_company_name, et_company_synopsis, et_company_linkman, et_company_phone, et_company_email; private String company_name, company_synopsis, company_linkman, company_phone, company_email, path, company_time, head; private LinearLayout company_post_photo; private TextView tv_submit; private ImageView company_post_text, company_post_text_one; private Activity activity; private UserBean user; private SimpleDateFormat mdata; private ImageLoader imageLoader = ImageLoader.getInstance(); private DisplayImageOptions options; // ? @SuppressLint("SdCardPath") private String filepath = "/sdcard/myheader"; private String picname = "newpic"; private static final int PHOTO_REQUEST_CAMERA = 1;// ? private static final int PHOTO_REQUEST_CUT = 3;// private static final int PHOTO_REQUEST_ALBUM = 2;// ? public static final int UPDATE_AVATAR_KEY = 5; private static final int KEY = 6; private boolean isfalse = true; private RelativeLayout btnback; private TextView toptitle; /** * ???? */ private MyReceiver receiver; private News_list_type.DataBean newstypebean; private LinearLayout ril_shenhechenggong, ril_renzhengshibai, ril_zhengzaishenhe; private Company_news_beans.DataBean combean; private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case KEY://? String key = (String) msg.obj; if (key != null) { try { JSONObject json = new JSONObject(key); String state = json.getString("status"); if ("success".equals(state)) { path = json.getString("data"); imageLoader.displayImage(NetBaseConstant.NET_HOST + "/" + path, company_post_text, options); } else { Toast.makeText(activity, "???", Toast.LENGTH_SHORT) .show(); } } catch (JSONException e) { e.printStackTrace(); } } else { Toast.makeText(activity, "???", Toast.LENGTH_SHORT).show(); } break; case COMPANYCERTIFY: if (msg.obj != null) { String result = (String) msg.obj; try { JSONObject json = new JSONObject(result); if ("success".equals(json.optString("status"))) { Toast.makeText(activity, "??", Toast.LENGTH_SHORT).show(); companyviews(); } else { Toast.makeText(activity, "?", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } else { Toast.makeText(activity, "?", Toast.LENGTH_SHORT).show(); } break; case GETCOMANYCERTIFY: if (msg.obj != null) { String result = (String) msg.obj; try { JSONObject json = new JSONObject(result); String data = json.getString("data"); if ("success".equals(json.optString("status"))) { JSONObject item = new JSONObject(data); combean = new Company_news_beans.DataBean(); combean.setId(item.getString("id")); combean.setUserid(item.getString("userid")); combean.setCreate_time(item.getString("create_time")); combean.setCompanyname(item.getString("companyname")); combean.setCompanyinfo(item.getString("companyinfo")); combean.setLinkman(item.getString("linkman")); combean.setPhone(item.getString("phone")); combean.setEmail(item.getString("email")); combean.setLicense(item.getString("license")); combean.setStatus(item.getString("status")); if (!"".equals(item.getString("status")) && item.getString("status") != null) { if ("1".equals(item.getString("status"))) { et_company_name.setText(combean.getCompanyname()); et_company_synopsis.setText(combean.getCompanyinfo()); et_company_linkman.setText(combean.getLinkman()); et_company_phone.setText(combean.getPhone()); et_company_email.setText(combean.getEmail()); imageLoader.displayImage(NetBaseConstant.NET_HOST + "/" + combean.getLicense(), company_post_text, options); // company_post_photo enable(false); tv_submit.setText(""); company_post_text_one.setVisibility(View.GONE); } else if ("0".equals(item.getString("status"))) { et_company_name.setText(""); et_company_synopsis.setText(""); et_company_linkman.setText(""); et_company_phone.setText(""); et_company_email.setText(""); enable(true); } else if ("-1".equals(item.getString("status"))) { ril_shenhechenggong.setVisibility(View.GONE); ril_renzhengshibai.setVisibility(View.VISIBLE); ril_zhengzaishenhe.setVisibility(View.GONE); tv_submit.setText("??"); // company_post_photo } else if ("-2".equals(item.getString("status"))) { ril_shenhechenggong.setVisibility(View.GONE); ril_renzhengshibai.setVisibility(View.GONE); ril_zhengzaishenhe.setVisibility(View.VISIBLE); identity_submit.setVisibility(View.GONE); } } } } catch (JSONException e) { e.printStackTrace(); } } else { et_company_name.setText(""); et_company_synopsis.setText(""); et_company_linkman.setText(""); et_company_phone.setText(""); et_company_email.setText(""); enable(true); } break; } } }; private LinearLayout identity_submit; private TextView title_top; private RelativeLayout btn_back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_identity_activity); activity = this; user = new UserBean(activity); indentityview(); } private void indentityview() { btnback = (RelativeLayout) findViewById(R.id.btn_back); btnback.setOnClickListener(this); toptitle = (TextView) findViewById(R.id.top_title); toptitle.setText("??"); ril_shenhechenggong = (LinearLayout) findViewById(R.id.ril_shenhechenggong); ril_renzhengshibai = (LinearLayout) findViewById(R.id.ril_renzhengshibai); ril_zhengzaishenhe = (LinearLayout) findViewById(R.id.ril_zhengzaishenhe); identity_submit = (LinearLayout) findViewById(R.id.identity_submit); identity_submit.setOnClickListener(this); et_company_name = (EditText) findViewById(R.id.company_name); et_company_synopsis = (EditText) findViewById(R.id.company_synopsis); et_company_linkman = (EditText) findViewById(R.id.company_linkman); et_company_phone = (EditText) findViewById(R.id.company_phone); et_company_email = (EditText) findViewById(R.id.company_email); company_post_photo = (LinearLayout) findViewById(R.id.company_post_photo); company_post_photo.setOnClickListener(this); company_post_text = (ImageView) findViewById(R.id.company_post_text);//text? company_post_text_one = (ImageView) findViewById(R.id.company_post_text_one); //?? mdata = new SimpleDateFormat("yyyy-MM-dd HH:mm"); company_time = formatdatatime(System.currentTimeMillis()); tv_submit = (TextView) findViewById(R.id.tv_submit); options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_shangchuan) .showImageOnFail(R.mipmap.ic_shangchuan).cacheInMemory(true).cacheOnDisc(true).build(); } private String formatdatatime(long time) { if (0 == time) { return ""; } return mdata.format(new Date(time)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.identity_submit: if ("".equals(tv_submit.getText().toString())) { enable(true); posynews(); } else if ("??".equals(tv_submit.getText().toString())) { enable(true); posynews(); } else if ("??".equals(tv_submit.getText().toString())) { ril_shenhechenggong.setVisibility(View.VISIBLE); ril_renzhengshibai.setVisibility(View.GONE); ril_zhengzaishenhe.setVisibility(View.GONE); tv_submit.setText("??"); enable(true); posynews(); } break; case R.id.company_post_photo: // getCheck(); ShowPickDialog(); break; case R.id.btn_back: finish(); break; } } //6.0??? private void getCheck() { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { //WRITE_EXTERNAL_STORAGE?? ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.CAMERA }, 100); } if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECEIVE_BOOT_COMPLETED) != PackageManager.PERMISSION_GRANTED) { //WRITE_EXTERNAL_STORAGE?? ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.RECEIVE_BOOT_COMPLETED }, 100); } if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { //WRITE_EXTERNAL_STORAGE?? ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.READ_PHONE_STATE }, 100); } if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //WRITE_EXTERNAL_STORAGE?? ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 100); } if (ContextCompat.checkSelfPermission(activity, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS) != PackageManager.PERMISSION_GRANTED) { //WRITE_EXTERNAL_STORAGE?? ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS }, 100); } } public void enable(boolean isfalse) { et_company_name.setEnabled(isfalse); et_company_synopsis.setEnabled(isfalse); et_company_linkman.setEnabled(isfalse); et_company_phone.setEnabled(isfalse); et_company_email.setEnabled(isfalse); company_post_photo.setEnabled(isfalse); } protected void ShowPickDialog() { new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_LIGHT) .setNegativeButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent intentFromGallery = new Intent(); intentFromGallery.setType("image/*"); // intentFromGallery.setAction(Intent.ACTION_PICK); startActivityForResult(intentFromGallery, PHOTO_REQUEST_ALBUM); } }).setPositiveButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // ???? String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File file = new File(path, "newpic.jpg"); intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } startActivityForResult(intentFromCapture, PHOTO_REQUEST_CAMERA); } }).show(); } private void posynews() { company_name = et_company_name.getText().toString(); company_synopsis = et_company_synopsis.getText().toString(); company_linkman = et_company_linkman.getText().toString(); company_phone = et_company_phone.getText().toString(); company_email = et_company_email.getText().toString(); if (company_name == null || "".equals(company_name)) { Toast.makeText(activity, "???", Toast.LENGTH_SHORT).show(); } else if (company_synopsis == null || "".equals(company_synopsis)) { Toast.makeText(activity, "?", Toast.LENGTH_SHORT).show(); } else if (company_linkman == null || "".equals(company_linkman)) { Toast.makeText(activity, "?", Toast.LENGTH_SHORT).show(); } else if (!isCanGetCode() || !ispHONE()) { Toast.makeText(activity, "???", Toast.LENGTH_SHORT).show();//? } else if (!isCanGetCodeemile()) { Toast.makeText(activity, "", Toast.LENGTH_SHORT).show();//? } else if (company_phone == null || "".equals(company_phone)) { Toast.makeText(activity, "??", Toast.LENGTH_SHORT).show(); } else if (company_email == null || "".equals(company_email)) { Toast.makeText(activity, "?", Toast.LENGTH_SHORT).show(); } else if (path == null || "".equals(path)) { Toast.makeText(activity, "", Toast.LENGTH_SHORT).show(); } else { if (HttpConnect.isConnnected(activity)) { new StudyRequest(activity, handler).UpdataCompanyCertify(user.getUserid(), company_name, company_synopsis, company_time, company_phone, company_email, company_linkman, path, COMPANYCERTIFY); } else { Toast.makeText(activity, R.string.net_erroy, Toast.LENGTH_SHORT).show(); } } } private Boolean isCanGetCode() { if (!RegexUtil.checkMobile(et_company_phone.getText().toString())) { return true; } return true; } private Boolean ispHONE() { if (!RegexUtil.checkPhone(et_company_phone.getText().toString())) { return true; } return true; } private Boolean isCanGetCodeemile() { if (!RegexUtil.checkEmail(et_company_email.getText().toString())) { return false; } return true; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_CANCELED) { switch (requestCode) { case PHOTO_REQUEST_CAMERA:// // ???? String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File tempFile = new File(path, "newpic.jpg"); startPhotoZoom(Uri.fromFile(tempFile)); } else { Toast.makeText(activity, "??", Toast.LENGTH_SHORT) .show(); } break; case PHOTO_REQUEST_ALBUM:// startPhotoZoom(data.getData()); break; case PHOTO_REQUEST_CUT: // ?? if (data != null) { getImageToView(data); } break; } } super.onActivityResult(requestCode, resultCode, data); } /** * ? * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // ? intent.putExtra("crop", "true"); // aspectX aspectY intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY ? intent.putExtra("outputX", 340); intent.putExtra("outputY", 340); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_REQUEST_CUT); } /** * ???? * * @param data */ private void getImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); Drawable drawable = new BitmapDrawable(this.getResources(), photo); picname = "avatar" + user.getUserid() + String.valueOf(new Date().getTime()); storeImageToSDCARD(photo, picname, filepath); if (HttpConnect.isConnnected(activity)) { new StudyRequest(activity, handler).updateUserImg(head, KEY); } else { Toast.makeText(activity, R.string.net_erroy, Toast.LENGTH_SHORT).show(); } } } /** * storeImageToSDCARD bitmapsdcard */ public void storeImageToSDCARD(Bitmap colorImage, String ImageName, String path) { File file = new File(path); if (!file.exists()) { file.mkdir(); } File imagefile = new File(file, ImageName + ".jpg"); try { imagefile.createNewFile(); FileOutputStream fos = new FileOutputStream(imagefile); colorImage.compress(Bitmap.CompressFormat.JPEG, 100, fos); head = imagefile.getPath(); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onResume() { super.onResume(); Log.i("onResume", "-------->onResume"); StatService.onPageStart(this, "??"); receiver = new MyReceiver(); IntentFilter filter = new IntentFilter("com.USER_ACTION"); registerReceiver(receiver, filter); companyviews(); } private void companyviews() { if (HttpConnect.isConnnected(activity)) { new StudyRequest(activity, handler).getCompanyCertify(user.getUserid(), GETCOMANYCERTIFY); } else { Toast.makeText(activity, R.string.net_erroy, Toast.LENGTH_SHORT).show(); } } @Override protected void onPause() { super.onPause(); // ??start???? StatService.onPageEnd(this, "??"); } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { newstypebean = (News_list_type.DataBean) intent.getSerializableExtra("fndinfo"); String title = intent.getStringExtra("title"); android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder( context); builder.setTitle("").setMessage(title).setCancelable(false) .setPositiveButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Bundle bundle = new Bundle(); bundle.putSerializable("fndinfo", newstypebean); Intent intent = new Intent(IdentityFragment_ACTIVITY.this, News_WebView_url.class); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); IdentityFragment_ACTIVITY.this.startActivity(intent); } }).setNegativeButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }).create().show(); context.unregisterReceiver(this); // AlertDialog alert = builder.create(); // alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // alert.show(); } } }