首页 > 编程学习 > Android 2.1 GPS定位和拍照功能代码
201011 月16

Android 2.1 GPS定位和拍照功能代码

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

    }
    @Override
    protected 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);
        }
    }

本文地址:https://wizzer.cn/archives/1358 , 转载请保留.

4 Responses to “Android 2.1 GPS定位和拍照功能代码”

  1. #1 android_Boy 回复 | 引用 Post:2011-01-08 01:12

    谢谢楼主的代码分享,但是有个困惑:就是我在虚拟机上运行GPS应用时总失败,楼主能给我发一份GPS的源码吗?Email:183434161@qq.com

    • #2 Wizzer 回复 | 引用 Post:2011-01-08 11:10

      访问GPS位置数据时需要ACCESS_FINE_LOCATION许可。AndroidManifest.xml 配置文件中加上 《uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” /》 。

  2. #3 Wizzer 回复 | 引用 Post:2011-04-11 16:21

    完整源码下载地址:http://download.csdn.net/source/3180960

  3. #4 小何 回复 | 引用 Post:2012-09-28 19:14

    我登陆的时候总是提示密码错误,想知道什么原因

发表评论