时间:2021-05-19
本文实例为大家分享了Android后台开启服务默默拍照的具体代码,供大家参考,具体内容如下
最近项目原因,需要编写一后台运行的程序,在给定时间间隔下进行拍照,关键技术主要是:1、开启服务;2、在不不预览的情况下,进行拍照操作。3、使用AlarmManager进行定时操作。
资源清单如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yang.testservice" android:versionCode="1" android:versionName="1.0" > <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera.any" /> <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.yang.service.LocalService" /> </application> </manifest>服务代码如下:
package com.yang.service; import java.io.IOException; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.hardware.Camera; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.view.SurfaceView; import android.widget.Toast; import com.yang.handle.PhotoHandler; import com.yang.testservice.MainActivity; import com.yang.testservice.R; public class LocalService extends Service { private AlarmManager am = null; private Camera camera; private final IBinder mBinder = new LocalBinder(); private NotificationManager mNM; private int NOTIFICATION = R.string.local_service_started; /** * Class for clients to access. Because we know this service always runs in * the same process as its clients, we don't need to deal with IPC. */ public class LocalBinder extends Binder { public LocalService getService() { return LocalService.this; } } @Override public void onCreate() { mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); showNotification(); init(); } private void init() { am = (AlarmManager) getSystemService(ALARM_SERVICE); camera = openFacingBackCamera(); // 注册广播 IntentFilter filter = new IntentFilter(); filter.addAction("com.vegetables_source.alarm"); registerReceiver(alarmReceiver, filter); Intent intent = new Intent(); intent.setAction("com.vegetables_source.alarm"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);// 马上开始,每1分钟触发一次 } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("LocalService", "Received start id " + startId + ": " + intent); return START_STICKY; } @Override public void onDestroy() { mNM.cancel(NOTIFICATION); cancelAlertManager(); if (camera != null) { camera.release(); camera = null; } Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT) .show(); } @Override public IBinder onBind(Intent intent) { return mBinder; } /** * Show a notification while this service is running. */ private void showNotification() { CharSequence text = getText(R.string.local_service_started); Notification notification = new Notification(R.drawable.stat_running, text, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent); mNM.notify(NOTIFICATION, notification); } private void cancelAlertManager() { Intent intent = new Intent(); intent.setAction("com.vegetables_source.alarm"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); am.cancel(pi); // 注销广播 unregisterReceiver(alarmReceiver); } BroadcastReceiver alarmReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if ("com.vegetables_source.alarm".equals(intent.getAction())) { if (camera != null) { SurfaceView dummy = new SurfaceView(getBaseContext()); try { camera.setPreviewDisplay(dummy.getHolder()); } catch (IOException e) { e.printStackTrace(); } camera.startPreview(); camera.takePicture(null, null, new PhotoHandler( getApplicationContext())); } } } }; private Camera openFacingBackCamera() { Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); ; for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { try { cam = Camera.open(camIdx); } catch (RuntimeException e) { e.printStackTrace(); } } } return cam; } }进行拍照存储的操作代码如下:
项目代码如下:
Android后台开启服务默默拍照
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了Android编辑头像功能的简单实例,可以实现拍照,图片选取,裁剪。拍照publicstaticvoidstartCamera(Fragmentfra
本文实例讲述了Android编程实现拍照功能的2种方法。分享给大家供大家参考,具体如下:Android系统的照相功能,已实现2种方法,可供大家参考:1.调用系统
本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题、参数设置相关问题以及前后摄像头语
Android判断后台服务是否开启的两种方法实例详解最近项目用到后台上传,就开启了一个服务service。但是刚开始用这种方法,有些机型不支持:酷派不支持。然后
这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:activ