Android(安卓)2.1 GPS定位和拍照功能代码
16lz
2022-04-22
1、GPS功能代码
private void getLocation(){LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 0, locationListener);}private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 // log it when the location changes if (location != null) { Lat.setText(String.valueOf(location.getLatitude())); Lon.setText(String.valueOf(location.getLongitude())); } } public void onProviderDisabled(String provider) { // Provider被disable时触发此函数,比如GPS被关闭 } public void onProviderEnabled(String provider) { // Provider被enable时触发此函数,比如GPS被打开 } public void onStatusChanged(String provider, int status, Bundle extras) { // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 }};
2、拍照功能代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Hide the window title.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); imageView = (ImageView) this.findViewById(R.id.iv1);Button button = (Button) this.findViewById(R.id.bt1);button.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);startActivityForResult(intent, 0);}}); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == 0 && resultCode == Activity.RESULT_OK) {this.imageView.setImageDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "temp.jpg").getAbsolutePath()));}}
3、退出程序确认
public boolean onKeyDown(int keyCode, KeyEvent event) {//按下键盘上返回按钮if(keyCode == KeyEvent.KEYCODE_BACK){new AlertDialog.Builder(Main.this)// Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。.setTitle("")// 设置对话框的标题.setMessage(" 确定退出? ")// 设置对话框的内容.setPositiveButton("确定",// 设置对话框的确认按钮 new DialogInterface.OnClickListener() {// 设置确认按钮的事件 public void onClick(DialogInterface dialog, int which) { //退出程序 android.os.Process.killProcess(android.os.Process.myPid()); }}).setNegativeButton("取消",// 设置对话框的取消按钮 new DialogInterface.OnClickListener() {// 设置取消按钮的事件 public void onClick(DialogInterface dialog, int which) { // 如果你什么操作都不做,可以选择不写入任何代码 dialog.cancel(); }}).show();return true;}else{return super.onKeyDown(keyCode, event);}}
更多相关文章
- android 字体大小,样式 ,像素的设置
- Android(安卓)Design Support Library(一):FloatingActionButton、T
- 第一章:初入Android大门(弹出对话框)
- Android点击按钮播放音效
- android 字体大小像素的设置
- Android(安卓)简单录音程序
- android 实现圆形头像
- android框架设计
- Android(安卓)---android:autoLink属性