flashlight not working,when device screen is off/sleep (android studio)

試著忘記壹切 提交于 2019-12-12 02:09:48

问题


i created a flashlight application,flashlight working. but flashlight not work when device screen power is off/sleep. i want flashlight continue on ,when device screen in off/sleep..

i am new for this site, i don't know,how upload full code, so i upload code in my blog site

manifest code

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" android:noHistory="true"            android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".About" android:noHistory="true"            android:screenOrientation="portrait"            >

    </activity>
</application>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-feature android:name="android.hardware.Camera"></uses-feature>

please see full code here


回答1:


Add below code inside if(!isOn) block:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

And the screen will never sleep. Then in the else block of that statement, add below code, which reenables the sleep feature:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

If you use onStop() method at the end for turning camera off when user closes the app, add the same code getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); In that block too.




回答2:


MainActivity.java code 

public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
Camera camera;
Camera.Parameters parameters;
boolean isflash=false;
boolean isOn=false;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageButton=(ImageButton) findViewById(R.id.imageButton); if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) { camera=Camera.open(); parameters=camera.getParameters(); isflash=true; } imageButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            if (isflash)
            {
                if (!isOn)
                {
                    imageButton.setImageResource(R.drawable.on);
                    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    isOn=true;

                }
                else
                {
                    imageButton.setImageResource(R.drawable.off);
                    parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    isOn=false;
                }

            }
            else
            {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Error........");
                builder.setMessage("Flashlight is not Available on this device...");
                builder.setPositiveButton("Ok",new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog,int which){
                    dialog.dismiss();
                    finish();

                }
            });
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        }

    });
}

@Override
protected void onStop() {
    super.onStop();
    if (camera!=null)
    {
        camera.release();
        camera=null;
    }
}

}



来源:https://stackoverflow.com/questions/38541837/flashlight-not-working-when-device-screen-is-off-sleep-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!