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);}}


更多相关文章

  1. android 字体大小,样式 ,像素的设置
  2. Android(安卓)Design Support Library(一):FloatingActionButton、T
  3. 第一章:初入Android大门(弹出对话框)
  4. Android点击按钮播放音效
  5. android 字体大小像素的设置
  6. Android(安卓)简单录音程序
  7. android 实现圆形头像
  8. android框架设计
  9. Android(安卓)---android:autoLink属性

随机推荐

  1. android view的缩放平移简单实现
  2. 增加Android可用内存
  3. android 常用api 接口签名验证
  4. android重要控件各种大全
  5. Android 横竖屏幕
  6. android UEventObserver的用法
  7. 谈谈 View 绘制流程
  8. android MTP 过滤指定的文件格式
  9. Android SDK4.0 离线安装方法
  10. 查看自己Android设备分辨率