当前位置: 首页 > 知识库问答 >
问题:

我正在android上制作一个torch应用程序

漆雕正奇
2023-03-14

创建一个简单的火炬应用程序,它可以放在相机闪光灯上并将其关闭。这个应用程序应该有一个简单的用户界面,可以打开闪光灯,也可以关闭它。连续使用1分钟后,用户界面应该提示用户他想继续使用火炬。如果用户说是,火炬将继续亮一分钟,这个循环将重复。如果用户说不,火炬将被推迟。

创建一个简单的火炬应用程序很容易,我已经制作了火炬应用程序,但问题中提到的第二件事是在1分钟后做出提示,我不明白有人能帮忙吗?

这是我的手电筒代码,有人能告诉我在哪里换吗?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    `enter code here`private boolean isLightOn=false;
    private Camera camera;
    private Button btn;


   @Override
   protected void onStop()
   {
       super.onStop();

       if(camera!= null)
           camera.release();
   }



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        Context context=this;

        PackageManager pk= context.getPackageManager();

        if(!pk.hasSystemFeature(PackageManager.FEATURE_CAMERA))
        {
            Log.e("err","Device has no camera");
            return;
        }
        camera=camera.open();
        final Parameters p= camera.getParameters();
        btn.setOnClickListener(new OnClickListener()
        {
         @Override
        public void onClick(View v)
         {

             if(isLightOn)
             {
                 Log.i("info","FlashLight is turn off");
                 p.setFlashMode(Parameters.FLASH_MODE_OFF);
                 camera.setParameters(p);
                 camera.stopPreview();
                 isLightOn=false;

             }
             else
             {
                 Log.i("info","FlashLight is turn On!");
                 p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                 camera.setParameters(p);
                 camera.startPreview();
                 isLightOn=true;

             }
         }

        });

    }
}

共有3个答案

慕弘伟
2023-03-14

要在特定延迟后运行某些代码,可以执行以下操作(其中1000是以毫秒为单位的延迟):

new Handler().postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        // code to show prompt goes here
    }
}, 1000);

您可以将其转换为一个方法,每次用户确认提示时都会调用该方法。

祁刚毅
2023-03-14
     if(!pk.hasSystemFeature(PackageManager.FEATURE_CAMERA))
            {
                Log.e("err","Device has no camera");
                return;
            }
            camera=camera.open();
            final Parameters p= camera.getParameters();
            btn.setOnClickListener(new OnClickListener()
            {
             @Override
            public void onClick(View v)
             {

                 if(isLightOn)
                 {
                     Log.i("info","FlashLight is turn off");
                     p.setFlashMode(Parameters.FLASH_MODE_OFF);
                     camera.setParameters(p);
                     camera.stopPreview();
                     isLightOn=false;

                 }
                 else
                 {
                     Log.i("info","FlashLight is turn On!");
                     p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                     camera.setParameters(p);
                     camera.startPreview();
                     isLightOn=true;
     new Handler().postDelayed(new Runnable() {
                        // Using handler with postDelayed called runnable run method
                        @Override
                        public void run() {
        //Show your Alert box here
        new AlertDialog.Builder(context) // you can use getApplicationContext() or your activityname.this as context
            .setTitle("Do You Want to continue")
            .setMessage("Are you sure you want to continue?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    //Do nothing, Torch continues
                }
             })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // torch off
 Log.i("info","FlashLight is turn off");
                 p.setFlashMode(Parameters.FLASH_MODE_OFF);
                 camera.setParameters(p);
                 camera.stopPreview();
                 isLightOn=false;
                }
             })
            .setIcon(android.R.drawable.ic_dialog_alert)
             .show();

        }
                    }, 60000); //60000milliseconds = 60 sec = 1min.

       // It will show alertbox after 1 min .

                 }
             }

            });

        }
    }
云锦
2023-03-14
  1. 首先创建一个具有开关小部件的XML。然后使用onOffSwitch=(Switch)findViewById(R.id.switch1)将其与Java连接起来

代码:

public class TorchActivity extends AppCompatActivity {

    CameraManager mCameraManager;
    String mCameraId;
    Switch onOffSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_torch);

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

        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = 1F;
        getWindow().setAttributes(layout);


        onOffSwitch = (Switch) findViewById(R.id.switch1);

        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            mCameraId = mCameraManager.getCameraIdList()[0];

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        Boolean isFlashAvailable = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (isFlashAvailable)
        {
            onOffSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onOffSwitch.isChecked()) {

                        try {
                            mCameraManager.setTorchMode(mCameraId, true);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    else{

                        try {
                            mCameraManager.setTorchMode(mCameraId, false);
                        }
                        catch (CameraAccessException e) {
                            e.printStackTrace();

                        }
                    }
                }
            });

        }else
        {
            Toast.makeText(this, "No Flash Support Found!", Toast.LENGTH_SHORT).show();
        }
}

//Use these permissions!

 Min SDK should be -  23
//<uses-permission android:name="android.permission.CAMERA" />
//<uses-permission android:name="android.permission.FLASHLIGHT" />
//<uses-feature android:name="android.hardware.camera" />
//<uses-feature android:name="android.hardware.camera.flash" />

 类似资料: