0

我已经创建了一个手电筒应用程序...一切正常...除了手电筒打开并且我按下后退按钮...它保持打开...并且不会停止...该怎么办? ..我使用了 onpause 方法,但它似乎不起作用...

另外,如果有人能向我解释参数的使用,我将不胜感激。我已从该程序的在线资源中获得帮助。我不明白参数方法...有人可以详细解释一下吗?提前致谢....:)

我的 XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/aaaaaaa"
tools:context=".Flash" >

<ImageButton
    android:id="@+id/ib2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" /></RelativeLayout>

我的 Flash.java:

    package com.potapptoes.flashlight;

    import android.os.Bundle;
    import android.os.PowerManager;
    import android.os.PowerManager.WakeLock;
    import android.app.Activity;
    import android.content.Context;
    import android.hardware.Camera;
    import android.hardware.Camera.Parameters;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageButton;

    public class Flash extends Activity implements OnClickListener {
Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    wl.acquire();
    initialize();
    ib1.setOnClickListener(this);
}

private void initialize() {
    // TODO Auto-generated method stub
    ib1 = (ImageButton) findViewById(R.id.ib2);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (cam == null) {
        cam = Camera.open();
        para = cam.getParameters();
        para.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(para);
    } else {
        para.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.setParameters(para);
        cam.release();
        cam = null;

    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release();
    finish();
}
    }

显现:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.potapptoes.flashlight.Flash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4

2 回答 2

1

You can try this

 para.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.setParameters(para);
        cam.release();
        cam = null;

in your onpause or maybe in onBackPressed and see if it works.

于 2012-12-11T11:26:09.693 回答
0

Just use your working code to turn off flashlight in onPause().

于 2012-12-11T11:26:26.917 回答