[英]Flashlight doesn't turn off when I press back button
我已經創建了一個手電筒應用程序...一切正常...除了每當手電筒打開並且我按下后退按鈕...它都保持打開狀態...並且不會停止...該怎么辦? ..我使用了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>
你可以試試這個
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
在您的onpause
或onBackPressed
查看其是否有效。
只需使用您的工作代碼在onPause()中關閉手電筒即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.