启动其他应用程序时手电筒关闭. Android的

启动其他应用程序时手电筒关闭. Android的,第1张

概述我正在使用Widget开发Flashlight应用.当我打开Widget手电筒的手电筒时,并且当我启动某些应用程序时,手电筒将关闭.为什么会这样呢?为什么我的手电筒无法在后台运行?我该如何预防?我希望仅由用户而不是系统关闭手电筒.这是我的小部件代码:@OverridepublicvoidonReceive(C

我正在使用Widget开发Flashlight应用.
当我打开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;    }    }}

解决方法:

这是在后台运行Falsh的全部代码.您需要将代码放入服务中的所有内容.然后从您的主要活动开始您的服务.

这是服务类:

public class ServiceFlash extends Service {private boolean isFlashOn = false;private Camera camera;Context context ;PackageManager pm;@OverrIDepublic voID onCreate() {    // Todo auto-generated method stub    context = getApplicationContext();    super.onCreate();}@OverrIDepublic 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);}@OverrIDepublic IBinder onBind(Intent intent) {    // Todo auto-generated method stub    return null;}

不要忘记将其添加到清单中:

<service        androID:name=".ServiceFlash"        androID:exported="false"/>

您的活动可能是这样的:
    公共类AppActivity扩展了Activity {
    private boolean isFlashOn = false;
    私人相机相机;
    私人按钮按钮;

@OverrIDeprotected voID onStop() {    super.onStop();    if (camera != null) {        camera.release();    }}@OverrIDeprotected voID onPause() {    // Todo auto-generated method stub    super.onPause();}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    Intent front_translucent = new Intent(this, ServiceFlash.class);    startService(front_translucent);}

}

您可以从这样的小部件类启动服务(尝试将代码放入小部件类的onReceive方法中):

 // Create intent     Intent serviceIntent = new Intent(context, mService.class);// start service context.startService(serviceIntent);

请享用..!

总结

以上是内存溢出为你收集整理的启动其他应用程序时手电筒关闭. Android的全部内容,希望文章能够帮你解决启动其他应用程序时手电筒关闭. Android的所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存