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