android手电筒小部件无法正常工作

时间:2015-02-26 03:59:54

标签: android

我正在尝试使用主屏幕上的小按钮创建一个具有打开和关闭闪光功能的小部件。

任何人都可以指出代码中的哪个位置我做错了。

主要的java文件是: -

package com.example.flashlightw;

import java.util.Arrays;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.RemoteViews;

public class Flash extends AppWidgetProvider {
    Camera camera = null;
    Parameters params = null;
    RemoteViews remoteViews = null;
    ComponentName watchWidget = null;
    ImageButton btnSwitch = null;
    AppWidgetManager appWidgetManager = null;
    boolean status = false;
    boolean hasFlash = true;

  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
     Log.i("FlashWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

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

      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent(context, Flash.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);
      views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);

      // To update a label

      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);

    }
  }

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

  public void onReceive(Context context, Intent intent) {

    remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget1);

    appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(new ComponentName(context, Flash.class), remoteViews);

    if(status)
    {
       status=false;
       remoteViews.setImageViewResource(R.id.imageButton1 , R.drawable.whitebackgroundlogo);
       turnOnFlash();
       //remoteViews.setInt(R.id.imageButton1, "setBackgroundResource", R.drawable.whitebackgroundlogo);
       //Toast.makeText(context,"Status==false-onclick",Toast.LENGTH_SHORT).show();
    }
    else
    {
        status = true;
        remoteViews.setImageViewResource(R.id.imageButton1, R.drawable.blackbackgroundlogo);
        turnOffFlash();
        //remoteViews.setInt(R.id.imageButton1, "setBackgroundResource", R.drawable.blackbackgroundlogo);
        //Toast.makeText(context,"Status==true--Ofclick",Toast.LENGTH_SHORT).show();
    }

    watchWidget = new ComponentName( context, Flash.class );

    (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
  }

    private void turnOnFlash() {
        try {
            if (!status) {
                if (camera == null || params == null) {
                    getCamera();
                }
                params = camera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(params);
                camera.startPreview();
            }
        } catch (RuntimeException e) {
            Log.e("Could not turn on Flash ", e.getMessage());
        }
    }

    private void turnOffFlash() {
        try {
            if (status) {
                if (camera == null || params == null) {
                    getCamera();
                }
                params = camera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(params);
                camera.stopPreview();
            }
        } catch (RuntimeException e) {
            Log.e("Could not turn off flash ", e.getMessage());
        }
    }
}

清单文件是: -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flashlightw"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name=".Flash" 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/widget1_info" />
        </receiver>
    </application>

</manifest>

布局文件是: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="2dp"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="52dp"
        android:layout_height="61dp"
        android:src="@drawable/blackbackgroundlogo" />

    <TextView
        android:id="@+id/widget1label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

任何帮助都会非常感激。 :)

0 个答案:

没有答案
相关问题