问题描述
我的工作用的Widget应用手电筒。
当我打开与小工具手电筒手电筒是,当我开始了一些应用程序,手电筒熄灭。
这究竟是为什么?为什么我的手电筒在后台运行?
我怎样才能prevent呢?我想手电筒仅由用户而不是系统被关闭。
这是我的code的部件:
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
RemoteViews意见=新的RemoteViews(context.getPackageName()
R.layout.widget_layout);
我的preF = preferenceManager.getDefaultShared preferences(背景);
如果(AppGlobals.getIsFlashOn()){
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_on);
}其他{
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_off);
} AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(上下文);
appWidgetManager.updateAppWidget(新组件名(背景下,
FlashlightWidgetProvider.class),意见); 如果(AppGlobals.getIsFlashOn()){
如果(getmCameraWidget()!= NULL){
flashOffWidget(); } 如果(Flashlight.getmCameraActivity()!= NULL){ flashOffApp();
Flashlight.flashlight_button
.setBackgroundResource(R.drawable.light_on); } Flashlight.turnMotorolaOff();
isLightOn = FALSE;
NotifyFlashlight(背景下,isLightOn); }其他{
尝试{
setmCameraWidget(Camera.open());
}赶上(例外五){
e.printStackTrace();
} 如果(getmCameraWidget()== NULL){
}其他{
setParamsWidget(getmCameraWidget()getParameters()); 清单<串GT; flashModes = getParamsWidget()
.getSupportedFlashModes(); 如果(flashModes == NULL){
返回;
}其他{
如果(计数== 0){
getParamsWidget()。setFlashMode(
Parameters.FLASH_MODE_OFF);
。getmCameraWidget()setParameters(getParamsWidget());
。getmCameraWidget()启动preVIEW();
AppGlobals.setIsFlashOn(真);
} 字符串flashMode = getParamsWidget()getFlashMode()。 如果(!Parameters.FLASH_MODE_TORCH.equals(flashMode)){ 如果(flashModes.contains(Parameters.FLASH_MODE_TORCH)){
getParamsWidget()。setFlashMode(
Parameters.FLASH_MODE_TORCH);
。getmCameraWidget()setParameters(getParamsWidget());
}其他{
getParamsWidget()。setFlashMode(
Parameters.FLASH_MODE_ON); 。getmCameraWidget()setParameters(getParamsWidget());
尝试{
getmCameraWidget()。自动对焦(
新AutoFocusCallback(){
公共无效onAutoFocus(
布尔的成功,
摄像头摄像头){
数= 1;
}
});
}赶上(例外五){
e.printStackTrace();
}
}
AppGlobals.setIsFlashOn(真);
isLightOn = TRUE;
NotifyFlashlight(背景下,isLightOn); }
}
} Flashlight.turnMotorolaOn();
} }
私人无效flashOffApp(){
。Flashlight.getmCameraActivity()停止preVIEW();
Flashlight.getmCameraActivity()版本()。
Flashlight.setmCameraActivity(NULL);
AppGlobals.setIsFlashOn(真);
计数= 0; }
私人无效flashOffWidget(){
。FlashlightWidgetReceiver.getmCameraWidget()停止preVIEW();
FlashlightWidgetReceiver.getmCameraWidget()版本()。
FlashlightWidgetReceiver.setmCameraWidget(NULL);
AppGlobals.setIsFlashOn(假);
计数= 0; } 公共静态相机getmCameraWidget(){
返回mCameraWidget;
} 公共静态无效setmCameraWidget(相机mCameraWidget){
FlashlightWidgetReceiver.mCameraWidget = mCameraWidget;
} 公共静态参数getParamsWidget(){
返回paramsWidget;
} 公共静态无效setParamsWidget(参数paramsWidgetSet){
paramsWidget = paramsWidgetSet;
}
}
}
下面是整个code在后台运行FALSH。你需要把你的code的服务。然后从您的主要活动启动服务。
下面是服务类:
公共类ServiceFlash延伸服务{
私人布尔isFlashOn = FALSE;
私人相机拍照;
上下文语境;
软件包管理系统时;
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
上下文= getApplicationContext();
super.onCreate();}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
// TODO自动生成方法存根
PM = context.getPackageManager(); 如果(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
Log.e(犯错,设备没有摄像头!);
Toast.makeText(getApplicationContext(),
您的设备不具备摄像头!,Toast.LENGTH_SHORT)
。显示(); 返回0;
} 相机= Camera.open();
最终的参数P = camera.getParameters(); //打开闪光灯
如果(isFlashOn){
Log.i(信息,火炬被关闭!);
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(P);
isFlashOn = FALSE;
}其他{
Log.i(信息,火炬被打开!);
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(P);
isFlashOn = TRUE;
}
返回super.onStartCommand(意向,旗帜,startId);}@覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
返回null;
}
不要忘了添加到您的清单:
<服务
机器人:名字=。ServiceFlash
机器人:出口=FALSE/>
您也许活动如下:
公共类AppActivity延伸活动{
私人布尔isFlashOn = FALSE;
私人相机拍照;
私人Button按钮;
@覆盖
保护无效的onStop(){
super.onStop(); 如果(相机!= NULL){
camera.release();
}
}@覆盖
保护无效的onPause(){
// TODO自动生成方法存根
super.onPause();}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
意图front_translucent =新意图(这一点,ServiceFlash.class); startService(front_translucent);
}
}
您可以开始从widget类服务类似这样的(尝试把这个code Widget类的onReceive方法中):
//创建意图
意图serviceIntent =新意图(背景下,mService.class);
//启动服务
context.startService(serviceIntent);
享受..!
I am working on Flashlight app with Widget.When I turn on Flashlight with Widget flashlight is on, and when I start some app, the flashlight turns off.Why is this happening? Why can't my Flashlight run in background? How can I prevent this? I want Flashlight to be turned off only by user not the system.
This is my code for widget:
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
myPref = PreferenceManager.getDefaultSharedPreferences(context);
if (AppGlobals.getIsFlashOn()) {
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_on);
} else {
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_off);
}
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context,
FlashlightWidgetProvider.class), views);
if (AppGlobals.getIsFlashOn()) {
if (getmCameraWidget() != null) {
flashOffWidget();
}
if (Flashlight.getmCameraActivity() != null) {
flashOffApp();
Flashlight.flashlight_button
.setBackgroundResource(R.drawable.light_on);
}
Flashlight.turnMotorolaOff();
isLightOn = false;
NotifyFlashlight(context, isLightOn);
} else {
try {
setmCameraWidget(Camera.open());
} catch (Exception e) {
e.printStackTrace();
}
if (getmCameraWidget() == null) {
} else {
setParamsWidget(getmCameraWidget().getParameters());
List<String> flashModes = getParamsWidget()
.getSupportedFlashModes();
if (flashModes == null) {
return;
} else {
if (count == 0) {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_OFF);
getmCameraWidget().setParameters(getParamsWidget());
getmCameraWidget().startPreview();
AppGlobals.setIsFlashOn(true);
}
String flashMode = getParamsWidget().getFlashMode();
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_TORCH);
getmCameraWidget().setParameters(getParamsWidget());
} else {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_ON);
getmCameraWidget().setParameters(getParamsWidget());
try {
getmCameraWidget().autoFocus(
new AutoFocusCallback() {
public void onAutoFocus(
boolean success,
Camera camera) {
count = 1;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
AppGlobals.setIsFlashOn(true);
isLightOn = true;
NotifyFlashlight(context, isLightOn);
}
}
}
Flashlight.turnMotorolaOn();
}
}
private void flashOffApp() {
Flashlight.getmCameraActivity().stopPreview();
Flashlight.getmCameraActivity().release();
Flashlight.setmCameraActivity(null);
AppGlobals.setIsFlashOn(true);
count = 0;
}
private void flashOffWidget() {
FlashlightWidgetReceiver.getmCameraWidget().stopPreview();
FlashlightWidgetReceiver.getmCameraWidget().release();
FlashlightWidgetReceiver.setmCameraWidget(null);
AppGlobals.setIsFlashOn(false);
count = 0;
}
public static Camera getmCameraWidget() {
return mCameraWidget;
}
public static void setmCameraWidget(Camera mCameraWidget) {
FlashlightWidgetReceiver.mCameraWidget = mCameraWidget;
}
public static Parameters getParamsWidget() {
return paramsWidget;
}
public static void setParamsWidget(Parameters paramsWidgetSet) {
paramsWidget = paramsWidgetSet;
}
}
}
Here is the whole code to run Falsh in background. all you need to put your code in a service. then start your service from your main activity.
Here is the service class:
public class ServiceFlash extends Service {
private boolean isFlashOn = false;
private Camera camera;
Context context ;
PackageManager pm;
@Override
public void onCreate() {
// TODO Auto-generated method stub
context = getApplicationContext();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(getApplicationContext(),
"Your device doesn't have camera!", Toast.LENGTH_SHORT)
.show();
return 0;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
// turn flash on
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
} else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
Don't forget add this to your manifest:
<service
android:name=".ServiceFlash"
android:exported="false"/>
Your activity maybe like this: public class AppActivity extends Activity { private boolean isFlashOn = false; private Camera camera; private Button button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent front_translucent = new Intent(this, ServiceFlash.class);
startService(front_translucent);
}
}
You can start your service from widget class like this (try to put this code inside onReceive method of widget class):
// Create intent
Intent serviceIntent = new Intent(context, mService.class);
// start service
context.startService(serviceIntent);
Enjoy..!
这篇关于当其他应用程序启动手电筒熄灭。 Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!