Tried to write widget for my Android app.
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());
}
}
}
Error:
09-11 14:39:40.595 80-373/? E/QualcommCameraHardware﹕ native_access_parm: error (No such file or directory): fd 30, type 1, length 32, status 0 09-11 14:39:46.255 6096-6096/net.testapp.flashlight E/Camera Error. Failed to Open. Error:﹕ Fail to connect to camera service 09-11 14:39:46.275 6096-6096/net.testapp.flashlight E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start receiver net.testapp.flashlight.Widget: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.handleReceiver(ActivityThread.java:2510) at android.app.ActivityThread.access$1500(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.e(Log.java:231) at net.testapp.flashlight.Widget.onReceive(Widget.java:87) at android.app.ActivityThread.handleReceiver(ActivityThread.java:2503) ... 10 more 09-11 14:39:48.715 80-6121/? E/mm-camera 8x-vfe﹕ Received VFE Stop ACK!!! ctrlCmd is NULL 09-11 14:40:45.935 5820-5921/com.android.vending E/PlayEventLogger﹕ Upload failed class java.net.UnknownHostException(Unable to resolve host "play.googleapis.com": No address associated with hostname) 09-11 14:41:59.745 6131-6131/com.android.providers.calendar E/Trace﹕ error opening trace file: No such file or directory (2) 09-11 14:42:01.265 6151-6151/com.cyanogenmod.lockclock E/Trace﹕ error opening trace file: No such file or directory (2) 09-11 14:42:01.355 6165-6165/com.google.android.calendar E/Trace﹕ error opening trace file: No such file or directory (2) 09-11 14:45:46.065 5820-5921/com.android.vending E/PlayEventLogger﹕ Upload failed class java.net.UnknownHostException(Unable to resolve host "play.googleapis.com": No address associated with hostname)