1

Hello folks,

I have an application that runs audio streaming via ExoPlayer, which will not run on the Android 12 platform. My application details:

ExoPlayer runs in PlayerService.java:

public void startMyAudio () {
    Notification notification = new NotificationCompat.Builder (this, getPackageName ())
    ...
    startForeground (9573, notification);
    player.prepare ();
    player.play ();
}

I run PlayerService.java from MainActivity.java, using the code:

private final ServiceConnection playerService_connection = new ServiceConnection () {
    @Override
    public void onServiceConnected (ComponentName classname, IBinder service) {
        playerService = IPlayerService.Stub.asInterface (service);
    }
    @Override public void onServiceDisconnected (ComponentName name) {playerService = null; }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    bindService(new Intent(this, PlayerService.class), playerService_connection, Context.BIND_AUTO_CREATE);
    ...
}

And, of course, control the service with AIDL interface:

playerService.startMyAudio();

Manifest permissions are:

<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name = "android.permission.WAKE_LOCK" />
<uses-permission android:name = "android.permission.FOREGROUND_SERVICE" />
...
<service
    android:name=".PlayerService"
    android:enabled="true"
    android:exported="true"
    android:foregroundServiceType="mediaPlayback|dataSync"
    android:permission="android.permission.INTERNET" />

I have no idea what to do next, because this works up to Android 12. What do I need to enable this app to run on Android 12? Was it enough to add Manifest permission as:

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

I ask this because I do not have the ability to run a virtual device on Android 12, nor an actual device on Android 12.

Thank you in advance.

1 Answers1

0

People,

I found a solution. You should first pin the notification when starting the service, and then anything is possible. I do it like this. First, I bind the service when I need it:

bindService(new Intent(this, PlayerService.class), playerService_connection, Context.BIND_AUTO_CREATE);

Else, everything is the same as above, except:

In MainActivity.java in ServiceConnection, I added only one line. Code below:

private final ServiceConnection playerService_connection = new ServiceConnection () {
    @Override
    public void onServiceConnected (ComponentName classname, IBinder service) {
        playerService = IPlayerService.Stub.asInterface (service);
        // This line below has been added
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(new Intent(THIS, PlayerService.class)); } 
    }
    @Override public void onServiceDisconnected (ComponentName name) { playerService = null; }
};

Too, in PlayerService.java be sure to call this:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    setMyNotification("Stand by."); // Your code instead
    return START_STICKY;
}

Now the foreground service is established, and I communicate with it via an interface. In this case, it is AIDL, although I prefer external ones that I personally create because that way I have more options at my disposal.

I came to this solution thanks to friends who helped with debugging online. Now I'm going to throw in the problem of installing AVD for Android 12 on my weird AMD processor.

Greeting.