一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android實現后臺開啟服務默默拍照功能

Android實現后臺開啟服務默默拍照功能

2022-03-01 15:36Snowball Android

這篇文章主要為大家詳細介紹了Android實現后臺開啟服務默默拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android后臺開啟服務默默拍照的具體代碼,供大家參考,具體內容如下

最近項目原因,需要編寫一后臺運行的程序,在給定時間間隔下進行拍照,關鍵技術主要是:1、開啟服務;2、在不不預覽的情況下,進行拍照操作。3、使用AlarmManager進行定時操作。

資源清單如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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>

服務代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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;
 }
}

進行拍照存儲的操作代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.yang.handle;
 
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.widget.Toast;
 
public class PhotoHandler implements PictureCallback {
 
 private final Context context;
 
 public PhotoHandler(Context context) {
  this.context = context;
 }
 
 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
 
  File pictureFileDir = getDir();
 
  if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
 
   Toast.makeText(context, "Can't create directory to save image.",
     Toast.LENGTH_LONG).show();
   return;
 
  }
 
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
  String date = dateFormat.format(new Date());
  String photoFile = "Picture_" + date + ".jpg";
 
  String filename = pictureFileDir.getPath() + File.separator + photoFile;
 
  File pictureFile = new File(filename);
  System.out.println("filename is "+ filename);
 
  try {
   FileOutputStream fos = new FileOutputStream(pictureFile);
   fos.write(data);
   fos.close();
   Toast.makeText(context, "New Image saved:" + photoFile,
     Toast.LENGTH_LONG).show();
  } catch (Exception error) {
   Toast.makeText(context, "Image could not be saved.",
     Toast.LENGTH_LONG).show();
  }
 }
 
 private File getDir() {
  File sdDir = Environment
   .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  return new File(sdDir, "ServiceCamera");
 }
}

項目代碼如下:

Android后臺開啟服務默默拍照

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/yangzl2008/article/details/9262505

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人女人天堂午夜视频 | 四虎综合九九色九九综合色 | 国产在线视频第一页 | 色444| 日本不卡高清免费v日本 | 日韩免费高清专区 | 91制片厂制作果冻传媒八夷 | 亚洲日本在线观看网址 | 国产成人亚洲精品一区二区在线看 | 性一交一无一伦一精一品 | 亚洲成色| 506070老熟肥妇bbwxx视频 500第一精品 | 91精品91| 调教麻麻成贱m | 无码国产成人午夜在线观看不卡 | 亚洲高清国产拍精品动图 | 国产福利片在线 易阳 | 成人欧美一区在线视频在线观看 | 久久99re2在线视频精品 | 久久一er精这里有精品 | 鸭子玩富婆流白浆视频 | 亚洲激情在线视频 | 毛片免费的 | 催眠白丝舞蹈老师小说 | 69欧美性猛交 | 波多野 在线 | 国产成人理在线观看视频 | 91精品国产麻豆国产自产在线 | 青青青手机视频在线观看 | 法国女佣系列在线播放 | 国产一区二区三区欧美 | 婷婷综合亚洲 | 成人私人影院www片免费高清 | 精品卡1卡2卡三卡免费网站 | 毛片a级放荡的护士hd | 999热在线精品观看全部 | 无人影院在线播放视频 | 被老头肉至怀孕小说 | 加勒比福利| 久久久高清国产999尤物 | 花房乱爱在线观看 |