Dhileepan C
Dhileepan C

Reputation: 51

We are facing an issue where the app crashes when minimized during livestream

java.lang.RuntimeException: Unable to start service com.app.buildyourownfanbase.helper.CallNotificationService1@76527d with Intent { cmp=com.app.buildyourownfanbase/.helper.CallNotificationService1 }: java.lang.SecurityException: Starting FGS with type microphone callerApp=ProcessRecord{ebefb57 27259:com.app.buildyourownfanbase/u0a752} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MICROPHONE] any of the permissions allOf=false [android.permission.CAPTURE_AUDIO_HOTWORD, android.permission.CAPTURE_AUDIO_OUTPUT, android.permission.CAPTURE_MEDIA_OUTPUT, android.permission.CAPTURE_TUNER_AUDIO_INPUT, android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT, android.permission.RECORD_AUDIO] and the app must be in the eligible state/exemptions to access the foreground only permission at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5286) at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2531) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:8919) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103) Caused by: java.lang.SecurityException: Starting FGS with type microphone callerApp=ProcessRecord{ebefb57 27259:com.app.buildyourownfanbase/u0a752} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MICROPHONE] any of the permissions allOf=false [android.permission.CAPTURE_AUDIO_HOTWORD, android.permission.CAPTURE_AUDIO_OUTPUT, android.permission.CAPTURE_MEDIA_OUTPUT, android.permission.CAPTURE_TUNER_AUDIO_INPUT, android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT, android.permission.RECORD_AUDIO] and the app must be in the eligible state/exemptions to access the foreground only permission at android.os.Parcel.createExceptionOrNull(Parcel.java:3069) at android.os.Parcel.createException(Parcel.java:3053) at android.os.Parcel.readException(Parcel.java:3036) at android.os.Parcel.readException(Parcel.java:2978) at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7234) at android.app.Service.startForeground(Service.java:862) at com.app.buildyourownfanbase.helper.CallNotificationService1.setCallNotification(CallNotificationService1.java:122) at com.app.buildyourownfanbase.helper.CallNotificationService1.onStartCommand(CallNotificationService1.java:46) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5268) at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2531)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:230)  at android.os.Looper.loop(Looper.java:319)  at android.app.ActivityThread.main(ActivityThread.java:8919)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)  Caused by: android.os.RemoteException: Remote stack trace: at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2741) at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2452) at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1797) at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:15948) at android.app.IActivityManager$Stub.onTransact$setServiceForeground$(IActivityManager.java:11974)

I expected the service to start without issues and handle the microphone-related functionality.

AndroidManifest.xml

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_HOTWORD" />
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
    <uses-permission android:name="android.permission.CAPTURE_MEDIA_OUTPUT" />
    <uses-permission android:name="android.permission.CAPTURE_TUNER_AUDIO_INPUT" />
    <uses-permission android:name="android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT" />
 <service
            android:enabled="true"
            android:name=".helper.CallNotificationService1"
            android:exported="true"
            android:foregroundServiceType="camera|microphone"

            android:permission="android.permission.RECORD_AUDIO">
            <intent-filter>
                <action android:name="ACTION_ACCEPT"/>
                <action android:name="ACTION_REJECT"/>
            </intent-filter>
        </service>

CallNotificationService1.java

 private static final String TAG = CallNotificationService1.class.getSimpleName();


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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service Started");
        setCallNotification(this);

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service Destroyed");


    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d(TAG, "onTaskRemoved");
        //stopStream1();
        Intent intent = new Intent("GPSLocationUpdates");
        intent.putExtra("Status", "App_kill");
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

        stopForeground(false);
        stopSelf();

     
    }
private void setCallNotification(Context context) {
        String appName = context.getString(R.string.app_name);
        long when = System.currentTimeMillis();

        Intent intent = new Intent(context, VideoCallActivity.class);
        intent.putExtra("notification", "true");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        int intents;
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.S) {
            intents = PendingIntent.FLAG_MUTABLE;
        } else {
            intents = PendingIntent.FLAG_IMMUTABLE;
        }
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(context, 0, intent, intents);
        Intent intent2 = new Intent(context, VideoCallActivity.class);
        intent2.putExtra("_ACTION_", "endcall");
        intent2.setAction("End Call");
        intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent endIntent = PendingIntent.getActivity(context, 0, intent2, intents);

        String channelId = context.getString(R.string.notification_channel_id);
        CharSequence channelName = context.getString(R.string.app_name);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            mNotifyManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId);
 Notification notification = mBuilder.build();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE){
          Log.d(TAG, "setCallNotification11111111111111111111: ");
startForeground(128,notification,ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
      }
       else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            startForeground(1000, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE | ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA /*| ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE*/);
        } else {
            startForeground(1000, notification);
        }}

Upvotes: 0

Views: 114

Answers (1)

devergent
devergent

Reputation: 46

First, since you're also using camera in the foreground service, make sure that you also request any of the permissions in this list:[android.permission.CAMERA, android.permission.SYSTEM_CAMERA]

If that doesn't fix the app crash, try adding an if-condition to check if all required permissions have been granted before starting the foreground service, for example:

if (hasAllRequiredPermissions()) {
    startForegroundService(Intent(this, CallNotificationService1::class.java))
} else {
    // Request permissions or show error to user
}

Upvotes: 0

Related Questions