FlashLight Widget崩溃

时间:2013-09-11 11:57:44

标签: android widget led flashlight

尝试为我的Android应用编写小部件。

的onUpdate:

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


    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent active = new Intent(context, Widget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    active.putExtra("msg", "FLASHLIGHT");

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);

    remoteViews.setOnClickPendingIntent(R.id.widget_button, actionPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

}

OnRecive:

@Override
public void onReceive(Context context, Intent intent) {
    String msg = "null";

    final String action = intent.getAction();
    if (ACTION_WIDGET_RECEIVER.equals(action)) {
        msg = intent.getStringExtra("msg");
        if (mCamera == null)
        {
            getCamera();
        }

            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        try
        {
            if(isFlashOn) { turnOffFlash(); }
            else { turnOnFlash(); }
        }
        catch (Exception e)
        {
            Log.e("ERROR --- > ",e.getMessage());
        }


    }
    super.onReceive(context, intent);

}


protected void turnOnFlash()
{
    params = mCamera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    mCamera.setParameters(params);
    isFlashOn = true;
}

protected void turnOffFlash()
{
    params = mCamera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    mCamera.setParameters(params);
    isFlashOn = false;
}

private void getCamera() {
    if (mCamera == null) {
        try {
            mCamera = Camera.open();
            params = mCamera.getParameters();
        }
        catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }

}

错误:

09-11 14:39:40.595 80-373 /? E / QualcommCameraHardware:native_access_parm:错误(没有这样的文件或目录):fd 30,类型1,长度32,状态0 09-11 14:39:46.255 6096-6096 / net.testapp.flashlight E /相机错误。打开失败。错误::无法连接到相机服务 09-11 14:39:46.275 6096-6096 / net.testapp.flashlight E / AndroidRuntime:FATAL EXCEPTION:main         java.lang.RuntimeException:无法启动接收器net.testapp.flashlight.Widget:java.lang.NullPointerException:println需要一条消息         在android.app.ActivityThread.handleReceiver(ActivityThread.java:2510)         在android.app.ActivityThread.access $ 1500(ActivityThread.java:153)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1323)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:137)         在android.app.ActivityThread.main(ActivityThread.java:5226)         at java.lang.reflect.Method.invokeNative(Native Method)         在java.lang.reflect.Method.invoke(Method.java:511)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)         at dalvik.system.NativeStart.main(Native Method)         引起:java.lang.NullPointerException:println需要一条消息         在android.util.Log.println_native(本机方法)         在android.util.Log.e(Log.java:231)         在net.testapp.flashlight.Widget.onReceive(Widget.java:87)         在android.app.ActivityThread.handleReceiver(ActivityThread.java:2503)         ......还有10个 09-11 14:39:48.715 80-6121 /? E / mm-camera 8x-vfe:收到VFE停止确认!!! ctrlCmd为NULL 09-11 14:40:45.935 5820-5921 / com.android.vending E / PlayEventLogger:上传失败的类java.net.UnknownHostException(无法解析主机“play.googleapis.com”:没有与主机名关联的地址) 09-11 14:41:59.745 6131-6131 / com.android.providers.calendar E / Trace:错误打开跟踪文件:没有这样的文件或目录(2) 09-11 14:42:01.265 6151-6151 / com.cyanogenmod.lockclock E / Trace:错误打开跟踪文件:没有这样的文件或目录(2) 09-11 14:42:01.355 6165-6165 / com.google.android.calendar E / Trace:错误打开跟踪文件:没有这样的文件或目录(2) 09-11 14:45:46.0​​65 5820-5921 / com.android.vending E / PlayEventLogger:上传失败的类java.net.UnknownHostException(无法解析主机“play.googleapis.com”:没有与主机名关联的地址)

2 个答案:

答案 0 :(得分:0)

您需要使用服务,例如:

Public class widget extends service{

@Override
Public void onCreate{
    Super.on create();
}

@Override
Public void onStart {
    Super.onStart ();

    If(isFlashOn) {
//enter code to open camera and turn on flash
}else{
//enter code to release camera and turn off flash
      Stop self();
   }
}

@Override
Public void onDestroy {
      Super.on destroy();
}

@Override
Public Ibinder onBind {
      Return null;
}
}

答案 1 :(得分:0)

删除“if(camera == null)”语句并使用getCamera();只是因为它已经在其方法中有这个陈述。

另外,添加“mCamera.startPreview;”在turnOnFLash代码中并在turnOffFlash代码中停止预览