⑴ 如何在android中后台开启摄像头默默拍照
(1)CameraActivity.java
package com.camera.test;import android.app.Activity;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;import android.widget.RelativeLayout;public class CameraActivity extends Activity {private CameraView view;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置横屏模式以及全屏模式view = new CameraView(this);// 通过一个surfaceview的view来实现拍照view.setId(1);view = new CameraView(this, this);setContentView(R.layout.camera_layout);RelativeLayout relative = (RelativeLayout) this.findViewById(R.id.ly);RelativeLayout.LayoutParams Layout = new RelativeLayout.LayoutParams(3, 3);// 设置surfaceview使其满足需求无法观看预览Layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);Layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);relative.addView(view, Layout);}}(2) CameraView.javapackage com.camera.test;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Point;import android.graphics.PointF;import android.hardware.Camera;import android.hardware.Camera.AutoFocusCallback;import android.hardware.Camera.ShutterCallback;import android.media.AudioManager;import android.media.FaceDetector;import android.media.FaceDetector.Face;import android.os.Environment;import android.os.Handler;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.widget.Toast;public class CameraView extends SurfaceView implements SurfaceHolder.Callback,Camera.PictureCallback{private SurfaceHolder holder;private Camera camera;private Camera.Parameters parameters;private Activity act;private Handler handler = new Handler();private Context context;private SurfaceView surfaceView;private AudioManager audio;private int current;public CameraView(Context context) {super(context);surfaceView = this;audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);final int current = audio.getRingerMode();audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);this.context = context;holder = getHolder();// 生成Surface Holderholder.addCallback(this);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 指定Push Bufferhandler.postDelayed(new Runnable() {@Overridepublic void run() {if (camera == null) {handler.postDelayed(this, 1 * 1000);// 由于启动camera需要时间,在此让其等两秒再进行聚焦知道camera不为空} else {camera.autoFocus(new AutoFocusCallback() {@Overridepublic void onAutoFocus(boolean success, Camera camera) {if (success) {camera.takePicture(new ShutterCallback() {// 如果聚焦成功则进行拍照@Overridepublic void onShutter() {}}, null, CameraView.this);} else {}}});}}}, 2 * 1000);}public CameraView(Context context, Activity act) {// 在此定义一个构造方法用于拍照过后把CameraActivity给finish掉this(context);this.act = act;}@Overridepublic void surfaceCreated(final SurfaceHolder holder) {// TODO Auto-generated method stubcamera = Camera.open();// 摄像头的初始化handler.postDelayed(new Runnable() {@Overridepublic void run() {if (holder != null) {try {camera.setPreviewDisplay(holder);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {handler.postDelayed(this, 1 * 1000);}}}, 2 * 1000);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stubparameters = camera.getParameters();camera.setParameters(parameters);// 设置参数camera.startPreview();// 开始预览}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub}public void onPictureTaken(byte[] data, Camera camera) {// 拍摄完成后保存照片try {Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");String time = format.format(date);//在SD卡上创建文件夹File file = new File(Environment.getExternalStorageDirectory()+ "/myCamera/pic");if (!file.exists()) {file.mkdirs();}String path = Environment.getExternalStorageDirectory()+ "/myCamera/pic/" + time + ".jpg";data2file(data, path);camera.setPreviewCallback(null);camera.stopPreview();camera.release();camera = null;holder.removeCallback(CameraView.this);audio.setRingerMode(current);act.finish();//uploadFile(path);} catch (Exception e) {}}private void data2file(byte[] w, String fileName) throws Exception {// 将二进制数据转换为文件的函数FileOutputStream out = null;try {out = new FileOutputStream(fileName);out.write(w);out.close();} catch (Exception e) {if (out != null)out.close();throw e;}}// private void uploadFile(String filePath)// 拍照过后上传文件到服务器// {// }}
⑵ android 如何让手机进入休眠状态,又如何唤
1.WakeLock主要代码如下:
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());//持有唤醒锁
wakeLock.setReferenceCounted(false);
wakeLock.acquire(30*1000);//30s亮屏
wakeLock.release();//释放锁,灭屏
2.FLAG_KEEP_SCREEN_ON代码如下
可使用 FLAG_KEEP_SCREEN_ON 替换WakeLock的方式.
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//亮屏
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//灭屏
⑶ Android中怎么实现打开文件时弹出一个打开方式可供选择的框。
这个是隐私Intent调用,没有指定明确的Activity,而是设置了条件,只要符合都可以响应。
像你说的这种是根据文件类型做条件来判断,可以通过Intent的setDataAndType方法实现。
这个Type是指MIME Type,网上有文件名后缀与MIME类型的对照表,可以参考。
提供一个打开内存储根目录下1.txt文件的样例代码,仅供参考(前提是没给txt文件设置默认的打开应用)
Intentintent=newIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory()+"/1.txt"),"text/plain");
startActivity(intent);
⑷ Android 应用中捕获返回事件,后退到的Acitivity 的内容自动经过刷新,应该如何实现
1、onCreate 方法只在activity一开始创建的时候执行。
2、也就是在该activity销毁后才能再次执行,假如当前activity上再打开一个activity,并且原来的activity已经销毁了,再返回原来的activity会重新执行onCreate
3、可以通过activity的生命周期的onStart方法或者是onResume方法对原来界面的数据进行刷新,也可以使用回调方法,或者是handler + Message
⑸ 求教android应用中设置屏幕是否常亮的方法
onCreate(){
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
onStop(){
getWindow().removeFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}