设为首页 加入收藏

TOP

Android后台开启服务默默拍照(一)
2014-11-24 03:14:25 来源: 作者: 【 】 浏览:4
Tags:Android 后台 开启 服务 默默 拍照

最近项目原因,需要编写一后台运行的程序,在给定时间间隔下进行拍照,关键技术主要是:1、开启服务;2、在不不预览的情况下,进行拍照操作。3、使用AlarmManager进行定时操作。


资源清单如下:








android:minSdkVersion="13"
android:targetSdkVersion="15" />


android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/title_activity_main" >








服务代码如下:


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 cancelAlert

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2013年华为校园招聘机试(未知) 下一篇Android中TextView中内容不换行的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)