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


1、GPS功能代码

[java]
  1. private void getLocation()  
  2.     {  
  3.         LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  4.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,  
  5.                  2000, locationListener);  
  6.   
  7.     }  
  8.     private final LocationListener locationListener = new LocationListener() {  
  9.         public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发   
  10.             // log it when the location changes   
  11.             if (location != null) {  
  12.                 Lat.setText(String.valueOf(location.getLatitude()));  
  13.                 Lon.setText(String.valueOf(location.getLongitude()));  
  14.   
  15.             }  
  16.         }  
  17.   
  18.         public void onProviderDisabled(String provider) {  
  19.         // Provider被disable时触发此函数,比如GPS被关闭   
  20.         }  
  21.   
  22.         public void onProviderEnabled(String provider) {  
  23.         //  Provider被enable时触发此函数,比如GPS被打开   
  24.         }  
  25.   
  26.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  27.         // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数   
  28.         }  
  29.     };  

2、拍照功能代码

[java]
  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.      // Hide the window title.   
  4.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  5.   
  6.         setContentView(R.layout.main);  
  7.         imageView = (ImageView) this.findViewById(R.id.iv1);  
  8.         Button button = (Button) this.findViewById(R.id.bt1);  
  9.         button.setOnClickListener(new OnClickListener() {  
  10.             public void onClick(View v) {  
  11.                 Intent intent = new Intent("Android.media.action.IMAGE_CAPTURE");  
  12.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri  
  13.                         .fromFile(new File(Environment  
  14.                                 .getExternalStorageDirectory(), "temp.jpg")));  
  15.                 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);  
  16.                 startActivityForResult(intent, 0);  
  17.             }  
  18.         });  
  19.   
  20.     }  
  21.     @Override  
  22.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  23.         if (requestCode == 0 && resultCode == Activity.RESULT_OK) {  
  24.             this.imageView.setImageDrawable(Drawable.createFromPath(new File(  
  25.                     Environment.getExternalStorageDirectory(), "temp.jpg")  
  26.                     .getAbsolutePath()));  
  27.   
  28.         }  
  29.     }  

3、退出程序确认

[java]
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  2.   
  3.         //按下键盘上返回按钮   
  4.         if(keyCode == KeyEvent.KEYCODE_BACK){  
  5.             new AlertDialog.Builder(Main.this)  
  6.             // Main.this视情况而定,这个一般是指当前显示的Activity对应的XML视窗。   
  7.             .setTitle("")// 设置对话框的标题   
  8.             .setMessage(" 确定退出? ")// 设置对话框的内容   
  9.             .setPositiveButton("确定",// 设置对话框的确认按钮   
  10.                 new DialogInterface.OnClickListener() {// 设置确认按钮的事件   
  11.                     public void onClick(DialogInterface dialog, int which) {  
  12.                         //退出程序   
  13.                         android.os.Process.killProcess(android.os.Process.myPid());  
  14.                 }})  
  15.             .setNegativeButton("取消",// 设置对话框的取消按钮   
  16.                 new DialogInterface.OnClickListener() {// 设置取消按钮的事件   
  17.                     public void onClick(DialogInterface dialog, int which) {  
  18.                         // 如果你什么操作都不做,可以选择不写入任何代码   
  19.                         dialog.cancel();  
  20.                 }}  
  21.             ).show();  
  22.   
  23.             return true;  
  24.         }else{  
  25.             return super.onKeyDown(keyCode, event);  
  26.         }  
  27.     }  

相关内容