您的位置:首页 > 移动开发 > Android开发

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

2012-02-03 18:23 447 查看


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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: