手电筒部件未关闭 [英] Flashlight widget does not turn off

查看:131
本文介绍了手电筒部件未关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造一切必要为我小部件存在和功能。即便如此在第一次点击,T做的事情应该是后来的图像得到改变,并说问题,不起作用。我希望它打开Flash,然后关闭它。
帮助将非常AP preciated。

FlashlightWidgetProvider

 公共类FlashlightWidgetProvider扩展AppWidgetProvider {       @覆盖
       公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
                       INT [] appWidgetIds){               意图接收器=新意图(背景下,FlashlightWidgetReceiver.class);
               receiver.setAction(COM_FLASHLIGHT);
               receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
               的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,接收机,0);               RemoteViews意见=新的RemoteViews(context.getPackageName()
                               R.layout.flash_widget);
               views.setOnClickPendingIntent(R.id.button,的PendingIntent);               appWidgetManager.updateAppWidget(appWidgetIds,意见);       }
}

FlashlightWidgetReceiver

 公共类FlashlightWidgetReceiver扩展广播接收器{
        私有静态布尔isLightOn = FALSE;
        私有静态相机摄像头;        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
                RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.flash_widget);                如果(isLightOn){
                        views.setImageViewResource(R.id.button,R.drawable.off);
                }其他{
                        views.setImageViewResource(R.id.button,R.drawable.on);
                }                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(上下文);
                appWidgetManager.updateAppWidget(新组件名(背景下,FlashlightWidgetProvider.class)
                                                                                 意见);                如果(isLightOn){
                        如果(相机!= NULL){
                                camera.stop preVIEW();
                                camera.release();
                                摄像头= NULL;
                                isLightOn = FALSE;
                        }                }其他{
                        //打开默认即面对镜头第一后部。
                        相机= Camera.open();                        如果(相机== NULL){
                                Toast.makeText(上下文,R.string.no_camera,Toast.LENGTH_SHORT).show();
                        }其他{
                                //设置火炬闪光模式
                                参数参数= camera.getParameters();
                                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                尝试{
                                        camera.setParameters(参数);
                                        camera.start preVIEW();
                                        isLightOn = TRUE;
                                }赶上(例外五){
                                        Toast.makeText(上下文,R.string.no_flash,Toast.LENGTH_SHORT).show();
                                }
                        }
                }
        }
}


解决方案

确认按钮资源指的是ImageView的,而不是一个常规的按钮。我只是在我的布局文件试过了这一点,先是用一个按钮,我得到了同样的问题,其中的部件将基本上崩溃,从主屏幕上删除自身。当我改变按钮,在布局文件中的ImageView中,code现在的工作。

我做了修改code从你一点,所以在情况下本身并不工作,这里是更新FlashlightWidgetProvider:

 公共类FlashlightWidgetProvider扩展AppWidgetProvider {    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())){
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(上下文);
            INT [] appWidgetIds = appWidgetManager.getAppWidgetIds(新单元名(上下文,的getClass()));            意图broadcastIntent =新意图(背景下,FlashlightWidgetReceiver.class);
            broadcastIntent.setAction(COM_FLASHLIGHT);
            broadcastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
            的PendingIntent的PendingIntent = PendingIntent.getBroadcast(背景下,
                                                                     0,
                                                                     broadcastIntent,
                                                                     PendingIntent.FLAG_UPDATE_CURRENT);            RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.flashlight);
            views.setOnClickPendingIntent(R.id.flashButton,的PendingIntent);            appWidgetManager.updateAppWidget(appWidgetIds,意见);
        }        super.onReceive(背景下,意图);
    }
}

此外,确保登记清单中的部件供应商和接收机正确(更换课程的相关作品与自己的,):

 <接收
        机器人:名字=com.example.stackoverflowtester.widget.FlashlightWidgetProvider
        机器人:标签=手电筒>
        &所述;意图滤光器>
            <作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>
        &所述; /意图滤光器>        &所述;元数据
            机器人:名字=android.appwidget.provider
            机器人:资源=@ XML / flashlight_widget_provider/>
    < /接收器>
    <接收机器人:名字=com.example.stackoverflowtester.widget.FlashlightWidgetReceiver>
        &所述;意图滤光器>
            <作用机器人:名字=COM_FLASHLIGHT/>
        &所述; /意图滤光器>
    < /接收器>

I have created everything necessary for my widget to exist and function. Even so at the first click, t does what it is supposed to but then image gets changed and says problem, and does not function. I want it to open flash and then close it. Help will be much appreciated.

FlashlightWidgetProvider

public class FlashlightWidgetProvider extends AppWidgetProvider {

       @Override
       public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                       int[] appWidgetIds) {

               Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
               receiver.setAction("COM_FLASHLIGHT");
               receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
               PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);

               RemoteViews views = new RemoteViews(context.getPackageName(),
                               R.layout.flash_widget);
               views.setOnClickPendingIntent(R.id.button, pendingIntent);

               appWidgetManager.updateAppWidget(appWidgetIds, views);

       }
}

FlashlightWidgetReceiver

public class FlashlightWidgetReceiver extends BroadcastReceiver {
        private static boolean isLightOn = false;
        private static Camera camera;

        @Override
        public void onReceive(Context context, Intent intent) {
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flash_widget);

                if(isLightOn) {
                        views.setImageViewResource(R.id.button, R.drawable.off);
                } else {
                        views.setImageViewResource(R.id.button, R.drawable.on);
                }

                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                appWidgetManager.updateAppWidget(new ComponentName(context,     FlashlightWidgetProvider.class),
                                                                                 views);

                if (isLightOn) {
                        if (camera != null) {
                                camera.stopPreview();
                                camera.release();
                                camera = null;
                                isLightOn = false;
                        }

                } else {
                        // Open the default i.e. the first rear facing camera.
                        camera = Camera.open();

                        if(camera == null) {
                                Toast.makeText(context, R.string.no_camera, Toast.LENGTH_SHORT).show();
                        } else {
                                // Set the torch flash mode
                                Parameters param = camera.getParameters();
                                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                try {
                                        camera.setParameters(param);
                                        camera.startPreview();
                                        isLightOn = true;
                                } catch (Exception e) {
                                        Toast.makeText(context, R.string.no_flash, Toast.LENGTH_SHORT).show();
                                }
                        }
                }
        }
}

解决方案

Make sure the button resource refers to an ImageView and not a regular Button. I just tried this out at first with a Button in my layout file and I was getting the same problem where the widget would basically crash and remove itself from the home screen. When I changed button to be an ImageView in the layout file, the code now works.

I did modify the code a bit from yours, so in case that doesn't work by itself, here is the updated FlashlightWidgetProvider:

public class FlashlightWidgetProvider extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));

            Intent broadcastIntent = new Intent(context, FlashlightWidgetReceiver.class);
            broadcastIntent.setAction("COM_FLASHLIGHT");
            broadcastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                                                                     0,
                                                                     broadcastIntent,
                                                                     PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flashlight);
            views.setOnClickPendingIntent(R.id.flashButton, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetIds, views);
        }

        super.onReceive(context, intent);
    }
}

Also, make sure to register the widget provider and receiver correctly in the manifest (replacing the relevant pieces with your own, of course):

    <receiver
        android:name="com.example.stackoverflowtester.widget.FlashlightWidgetProvider"
        android:label="Flashlight" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/flashlight_widget_provider" />
    </receiver>
    <receiver android:name="com.example.stackoverflowtester.widget.FlashlightWidgetReceiver" >
        <intent-filter>
            <action android:name="COM_FLASHLIGHT" />
        </intent-filter>
    </receiver>

这篇关于手电筒部件未关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆