易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Android 2.1 GPS定位和拍照功能代码
1、GPS功能代码
[java]
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、拍照功能代码
[java]
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、退出程序确认
[java]
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);
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图