トグルをクリックして懐中電灯をオンにするアプリケーションを開発しようとしています。Webで見つけたすべての方法を試しましたが、nexus 5では成功しませんでした。機能する唯一のアプリケーションはcyanogenmodトーチですが、そのコードを取得してアプリケーションに記述しても、クリックすると開くという通知が作成されますcyanogenmod torch アプリであり、私のものではありません! これは問題です!!nexus 5でも機能する簡単なコードはありますか?
public void turnOffFlashLight() {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();
}
}
public void turnOnFlashLight() {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
cam.setPreviewTexture(new SurfaceTexture(0));
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception throws in turning on flashlight.", Toast.LENGTH_SHORT).show();
}
}
編集:logcat
04-09 21:52:21.967: E/AndroidRuntime(5346): FATAL EXCEPTION: main
04-09 21:52:21.967: E/AndroidRuntime(5346): Process: com.lob.twixlight, PID: 5346
04-09 21:52:21.967: E/AndroidRuntime(5346): android.util.SuperNotCalledException: Activity {com.lob.twixlight/com.lob.twixlight.MainActivityTwixLight} did not call through to super.onStart()
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.Activity.performStart(Activity.java:5243)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.os.Looper.loop(Looper.java:136)
04-09 21:52:21.967: E/AndroidRuntime(5346): at android.app.ActivityThread.main(ActivityThread.java:5081)
04-09 21:52:21.967: E/AndroidRuntime(5346): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:52:21.967: E/AndroidRuntime(5346): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 21:52:21.967: E/AndroidRuntime(5346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-09 21:52:21.967: E/AndroidRuntime(5346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-09 21:52:21.967: E/AndroidRuntime(5346): at dalvik.system.NativeStart.main(Native Method)
MainActivity 全体
public class MainActivity extends Activity implements Callback {
private ImageView imageView3;
private ImageButton mButtonOn;
private Camera cam;
private SurfaceHolder mHolder ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
new AlertDialog.Builder(this);
setContentView(R.layout.activity_main);
mButtonOn = (ImageButton) findViewById(R.id.my_button);
imageView3 = (ImageView) findViewById(R.id.imageView3);
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
mHolder = preview.getHolder();
mHolder.addCallback(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void info(View v) {
Intent intent = new Intent(this, FullscreenActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in, R.anim.out);
}
protected void onStart() {
super.onStart ();
SurfaceView preview = (SurfaceView)findViewById(R.id.PREVIEW);
mHolder = preview.getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mHolder = holder;
try {
Log.i("SurfaceHolder", "setting preview");
cam.setPreviewDisplay(mHolder);
} catch (IOException e){
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
cam.stopPreview();
mHolder = null;
}
}