在检查设备是否支持手电筒后,不能在if语句中放置布尔值

时间:2016-11-04 21:03:13

标签: java android android-package-managers flashlight

我检查了设备是否支持flash并且我做了if语句,然后我收到错误。有谁知道这方面的解决方案?它说console.error(err.stack)是一个未知的类。

hasFlash

}

3 个答案:

答案 0 :(得分:1)

有问题的代码不在函数中。你的意思是把它放在你的onCreate吗?如果是这样,你的右支撑位置错误:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    flashlightButton = (ImageButton) findViewById(R.id.flashOnOffButton);
    flashlightOnOrOff = false; 
} // REMOVE THIS 

//Error if device does not have flashlight 
    boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    if(hasFlash==false)
    {
        AlertDialog dialo = new AlertDialog.Builder(Flashlight.this).create();
        dialo.setTitle("Error");
        dialo.setMessage("Sorry your device does not have flashlight");
        dialo.setButton(BUTTON_POSITIVE, "OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        dialo.show();
    }
} // THIS IS WHERE YOUR ONCREATE CLOSING BRACE GOES

答案 1 :(得分:0)

试试这个

hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

if (!hasFlash) {    
// device doesn't support flash    
// Show alert message and close the application  

    AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();    
    alert.setTitle("Error");    
    alert.setMessage("Sorry, your device doesn't support flash light!");  

    alert.setButton("OK", new DialogInterface.OnClickListener() {    
    public void onClick(DialogInterface dialog, int which) {

        // closing the application    
        finish();    
    }

    });

    alert.show();    
    return;

}

答案 2 :(得分:-1)

在AndroidManifest.xml上添加以下权限

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

将以下导入放在MainActivity.java

之上
package com.stackoverflow.myapplication;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import static android.content.DialogInterface.BUTTON_POSITIVE;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        AlertDialog dlg = new AlertDialog.Builder(this).create();
        if (hasFlash) {
            dlg.setTitle("Done");
            dlg.setMessage("Your device does have flashlight");
        }
        else {
            dlg.setTitle("Error!");
            dlg.setMessage("Sorry your device does not have flashlight!");
        }

        dlg.setButton(BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //finish();
            }
        });
    dlg.show();
    }

}

有关详细信息,请查看here