在Android手机的状态栏上显示一个摄像头是否在使用的图标(防止偷拍)

使用下面的接口,看下打开关闭camera是否能得到通知,然后做一个service

CameraManager
AvailabilityCallback

public static abstract class AvailabilityCallback {
607
608        /**
609         * A new camera has become available to use.
610         *
611         * <p>The default implementation of this method does nothing.</p>
612         *
613         * @param cameraId The unique identifier of the new camera.
614         */
615        public void onCameraAvailable(@NonNull String cameraId) {
616            // default empty implementation
617        }
618
619        /**
620         * A previously-available camera has become unavailable for use.
621         *
622         * <p>If an application had an active CameraDevice instance for the
623         * now-disconnected camera, that application will receive a
624         * {@link CameraDevice.StateCallback#onDisconnected disconnection error}.</p>
625         *
626         * <p>The default implementation of this method does nothing.</p>
627         *
628         * @param cameraId The unique identifier of the disconnected camera.
629         */
630        public void onCameraUnavailable(@NonNull String cameraId) {
631            // default empty implementation
632        }
633    }

使用通知管理器来显示UI图标:

//创建一个通知管理器
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android应用程序,可以调用手机摄像头: 1. 首先,打开Android Studio,创建一个新的空白项目。 2. 在项目中创建一个新的Activity,命名为CameraActivity。 3. 打开CameraActivity.java文件,添加以下代码: ``` import android.content.Intent; import android.os.Bundle; import android.provider.MediaStore; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class CameraActivity extends AppCompatActivity { private static final int REQUEST_IMAGE_CAPTURE = 1; private Button btnCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); btnCamera = findViewById(R.id.btn_camera); btnCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dispatchTakePictureIntent(); } }); } private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // The user has taken a photo and it is saved to the phone's gallery } } } ``` 4. 在res/layout文件夹中创建一个名为activity_camera.xml的布局文件,并添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CameraActivity"> <Button android:id="@+id/btn_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take a picture" android:layout_centerInParent="true"/> </RelativeLayout> ``` 5. 现在你可以运行你的应用程序,并在点击“Take a picture”按钮时调用摄像头。当用户拍照后,照片将保存在手机的图库中。 注意:在运行应用程序之前,请确保你的手机上已经安装了相机应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值