无法获得Android闪光灯小工具打开闪光灯

时间:2013-03-12 03:34:14

标签: android flash widget galaxy light

我正在尝试为Galaxy nexus制作一个闪光灯小部件,并且已经停留在这个问题上。我有一个小部件,其中包含一个功能类似于切换的按钮。但是我无法按下按钮来打开闪光灯。该小部件用作使用toasts的切换,但是一旦我添加了与flash通信的代码行,它就会崩溃。因此,如果您只在每个if语句中保留toast代码,那么toasts将起作用。我知道我应该使用SurfaceHolder.Callback但我不知道如何在扩展AppWidgetProvider的类上实现它。如果有人可以请查看我的代码并尝试帮我制作这个小部件,我将非常感激。我只是不知道如何使用appwidgetprovider类实现它。

只有在我有以下情况时才会出现此错误:

//TURN FLASH OFF
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();

//TURN FLASH ON
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);                    
cam.setParameters(p);
cam.startPreview();

如果我没有放入这些代码行,则小部件运行时没有错误,只显示祝酒词。这意味着它访问onReceive()方法。

我正在使用星系连接。

public class FlashWidget extends AppWidgetProvider {

Camera cam;
RemoteViews view;
private static final String ACTION_WIDGET_RECEIVER = "Action";
private static boolean isLightOn = false;



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

        final int n = appWidgetIds.length;
        view = new RemoteViews(context.getPackageName(), R.layout.widget_lay);

        for (int i = 0; i < n; i++) {  
               int appWidgetId = appWidgetIds[i];  

               Intent intent = new Intent(context, FlashWidget.class);
               intent.setAction(ACTION_WIDGET_RECEIVER);

               PendingIntent pend = PendingIntent.getBroadcast(context, 0, intent, 0);

               view.setOnClickPendingIntent(R.id.button1, pend);


               appWidgetManager.updateAppWidget(appWidgetId, view);
        }


    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action =intent.getAction();

        if(intent.getAction().equals(action)) {


            //YES ITS ON, so turn it OFF
            if(isLightOn) {

                isLightOn = false;
                //TURN FLASH OFF
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                Toast.makeText(context, "Flash OFF", Toast.LENGTH_SHORT).show();

            //NO ITS OFF, SO LETS TURN IT ON
            } else {

                isLightOn = true;
                //TURN FLASH ON
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                cam.setParameters(p);
                cam.startPreview();
                Toast.makeText(context, "Flash ON", Toast.LENGTH_SHORT).show();

            }

            super.onReceive(context, intent);

        }


    } 
}

的AndroidManifest.xml

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>



<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.flash.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver 
        android:name=".FlashWidget"
        android:label="Flash">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="com.example.flash.ACTION_WIDGET_RECEIVER"/>
            <action android:name="android.appwidget.action.APPWIDGET_ENABLED"/>
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_in"/>
   </receiver>
</application>

</manifest>

这是我尝试将小部件放在主屏幕上时出现的错误

E/AndroidRuntime(10346): FATAL EXCEPTION: main
E/AndroidRuntime(10346): java.lang.RuntimeException: Unable to start receiver com.example.flash.FlashWidget: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
E/AndroidRuntime(10346): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
E/AndroidRuntime(10346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)

3 个答案:

答案 0 :(得分:0)

你必须在manifest.xml文件中加入一些条件

<receiver
        android:name=".FlashWidget "
        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/your xml file" />
    </receiver>

如果您发现任何问题,请告诉我。

答案 1 :(得分:0)

Add camera.open();
if(isLightOn){           
        Log.i("info", "torch is turned off!");
        parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
        Global.camera.setParameters(parameter);
        ibtnSwith.setBackgroundResource(R.drawable.switch_off);
        isLightOn = false;


    } else {
        parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
        Global.camera.setParameters(parameter);                 
        isLightOn = true;
        ibtnSwith.setBackgroundResource(R.drawable.switch_on);
        playsound();
        Log.i("Home.java", "Torch is turned on!");  
    }

答案 2 :(得分:0)

在nexus设备中它应该添加一个转储表面以使闪存工作,我搜索了很长一段时间,我尝试了互联网上的每个代码,但闪存没有打开,最后我发现解决方案如下:< / p>

private void turnLEDOn() 
{
    // In order to work, the camera needs a surface to turn on.
    // Here I pass it a dummy Surface Texture to make it happy.
    camera = Camera.open();
    camera.setPreviewTexture(new SurfaceTexture(0));
    camera.startPreview();
    Parameters p = camera.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);`enter code here`
}

private void turnLEDOff()
{
    if (camera != null)
    {
        // Stopping the camera is enough to turn off the LED
        camera.stopPreview();
        camera.release();
        camera = null;
    } else
        throw new NullPointerException("Camera doesn't exist to turn off.");

}