我需要一个服务来打开设备上的摄像头吗?

s1ag04yj  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(721)

android应用程序在使用仿真器时运行良好-打开相机并上载图像没有使用任何前台服务,但当我在设备上测试时,出现以下错误:

W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: lu@b3804ca
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1766)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1810)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
        at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):1)
        at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):2)
        at lv.E(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):9)
        at lf.a(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):3)
        at ef.run(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):3)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at iy.run(:com.google.android.gms.dynamite_measurementdynamite@204516081@20.45.16 (120400-0):5)

有人能给我解释一下,然后给我指出关于服务的正确方向吗?我已经阅读了文档,理解我可能需要声明前台服务类型,但是我不确定是否要创建一个新的myservice类以及它如何与清单文件交互
清单文件:

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       package="com.example.sailing_tracker">
       <uses-permission android:name="android.permission.INTERNET" />
       <uses-permission android:name="android.permission.CAMERA" />
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

       <application
           android:usesCleartextTraffic="true"
           android:allowBackup="true"
           android:icon="@mipmap/ic_launcher"
           android:label="@string/app_name"
           android:roundIcon="@mipmap/ic_launcher_round"
           android:supportsRtl="true"
           android:theme="@style/Theme.AppCompat.Light.NoActionBar"
           tools:targetApi="m">
           <activity android:name=".LoginActivity"/>
           <activity android:name=".DashboardActivity" />
           <activity android:name=".RegisterActivity" />
           <activity android:name=".MainActivity">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
          <service android:name=".ForegroundCameraService"/>
       </application>
   </manifest>

服务

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

public class ForegroundCameraService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
8yoxcaq7

8yoxcaq71#

请给予如下前台服务权限

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   package="com.example.sailing_tracker">
   <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <application
       android:usesCleartextTraffic="true"
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/Theme.AppCompat.Light.NoActionBar"
       tools:targetApi="m">
       <activity android:name=".LoginActivity"/>
       <activity android:name=".DashboardActivity" />
       <activity android:name=".RegisterActivity" />
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category  android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

   </application>
   </manifest>

相关问题