Android Question InstantiationException

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am working making a music player for Android Auto.

Auto sees my player (I believe I have everything defined properly in the manifest)

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="27"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

SetManifestAttribute("android:installLocation", "auto")
SetActivityAttribute(Main, android:launchMode, "singleInstance")

AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.BLUETOOTH)
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.BROADCAST_STICKY)
AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.CONNECTIVITY_INTERNAL)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.MEDIA_CONTENT_CONTROL)
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission(android.permission.MODIFY_PHONE_STATE)
AddPermission(android.permission.PERSONAL_MEDIA)
AddPermission(android.permission.PREVENT_POWER_KEY)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_SETTINGS)
AddPermission(android.permission_group.AUDIO_SETTINGS)
AddPermission(android.permission_group.BLUETOOTH_NETWORK)



AddManifestText("android.permission-group.HARDWARE_CONTROLS")

AddApplicationText(<meta-data android:name="com.google.android.gms.car.application"
                         android:resource="@xml/automotive_app_desc"/>
)

        
AddServiceText(MyMediaBrowserService,
               android:enabled="true"
               android:exported="true"
               android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE"
                     <intent-filter>
                         <action android:name="android.media.browse.MediaBrowserService" />
                     </intent-filter>
 )

AddReceiverText(MyMediaBrowserService, 
                     <intent-filter>
                       <action android:name="com.google.android.gms.car.media.STATUS" />
                   </intent-filter>)

AddApplicationText(<receiver android:name="mymediabrowserservice$MyReceiver">
                       <intent-filter android:priority="2147483647" >
                           <action android:name="com.google.android.gms.car.media.STATUS" />
                           <action android:name="android.intent.action.MEDIA_BUTTON" />                          
                           <action android:name="com.sec.android.app.music.musicservicecommand.play"/>
                           <action android:name="com.sec.android.app.music.musicservicecommand.pause"/>
                           <action android:name="com.sec.android.app.music.musicservicecommand.playprevious"/>
                           <action android:name="com.sec.android.app.music.musicservicecommand.playnext"/>                          
                       </intent-filter>
                   </receiver>)

Every time I try to create a class MediaBrowserServiceCompat I get this error
B4X:
Error on MyMediaBrowserService:java.lang.InstantiationException: java.lang.Class<android.support.v4.media.MediaBrowserServiceCompat> cannot be instantiated

In MyMediaBrowserService (which is a service) This is what I am doing

B4X:
               Try                                            'android.support.v4.media.MediaBrowserServiceCompat
                   Dim CallBack    As JavaObject
                  
                   MediaBrowserService.InitializeNewInstance("android.support.v4.media.MediaBrowserServiceCompat", Null)

                   Log("MyMediaBrowserService Initialize")

                   CallBack.InitializeNewInstance(Application.PackageName & ".mymediabrowserservice.MyMediaBrowserService", Null)                  
               Catch
                   Log("Error on MyMediaBrowserService:" &LastException.Message)
               End Try

This code fails when I do the InitializeNewInstance

this is my MediaBrowserService Class
B4X:
#if java
import android.support.v4.media.session.MediaSessionCompat.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.IntentWrapper;
import android.os.Bundle;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.support.v4.media.MediaBrowserServiceCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
//'import android.support.v7.media.MediaRouter;
import android.os.ResultReceiver;
import android.content.IntentFilter;
import java.util.ArrayList;
import java.util.List;
import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.session.PlaybackState;
import android.service.media.MediaBrowserService.Result;


public   class  MyMediaBrowserService extends MediaBrowserServiceCompat   {


       private  final String MEDIA_ID_ROOT            = "__ROOT__";
       private  final String MY_EMPTY_MEDIA_ROOT_ID    = "__Empty_Root__";

       private MediaSessionCompat mSession;
       private MyMediaSessionCallback mCallback;

     public MyMediaBrowserService() {
                 super();
                   BA.Log("MyMediaBrowserService - MyMediaBrowserService");    
                
                 mSession  = new MediaSessionCompat(this, "MusicService");
               mCallback = new MyMediaSessionCallback();
                 mSession.setCallback(mCallback);
                
                 mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                                   MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
                                   MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
                                  
                 setSessionToken(mSession.getSessionToken());              
                
                   BA.Log("MyMediaBrowserService - MyMediaBrowserService Complete");                    
       }
      
@Override
   public void onCreate() {
//                super();
                 BA.Log("MyMediaBrowserService - onCreate");    
//              
//               mSession  = new MediaSessionCompat(this, "MusicService");
//             mCallback = new MyMediaSessionCallback();
//               mSession.setCallback(mCallback);
//              
//               mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
//                                 MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
//                                 MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
//                                
//               setSessionToken(mSession.getSessionToken());              
//              
//                 BA.Log("MyMediaBrowserService - MyMediaBrowserService Complete");                                    
   }
    

@Override
   public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
                                 BA.Log("MyMediaBrowserService - OnGetRoot");    
                               processBA.raiseEventFromUI(getObject(), "media_ongetroot", clientPackageName);
                              
                               return new BrowserRoot(MEDIA_ID_ROOT, null);
   }
  
@Override  
   public void onLoadChildren(String parentMediaId, Result<List<MediaItem>> result) {  
                              // I promise we’ll get To browsing
                            
                                 BA.Log("MyMediaBrowserService - OnLoadChildren");                                
                               processBA.raiseEventFromUI(getObject(), "media_onloadchildren", parentMediaId);
                            
                                   result.sendResult(null);
   }  

  
public   class MyMediaSessionCallback extends MediaSessionCompat.Callback {
       public MyMediaSessionCallback() {
           }
      
           public void onCommand(String command, Bundle args, ResultReceiver cb) {
                                 processBA.raiseEventFromUI(null, "media_oncommand", command);
           }
      
           public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
                                   IntentWrapper baIntent = new IntentWrapper();
                               baIntent.setObject(mediaButtonIntent);
                                 Boolean b = (Boolean) processBA.raiseEvent(null, "media_onbutton", baIntent);
                               return b == null ? false : b;
           }
      
   public void onStop() {
                       processBA.raiseEventFromUI(getObject(), "media_onstop", null);
                       return;  
   }
  
   public void onPlay() {
                       processBA.raiseEventFromUI(getObject(), "media_onplay", null);
                       return;  
   }

   public void onPause() {
                       processBA.raiseEventFromUI(getObject(), "media_onpause", null);
                       return;  
   }

   public void onSkipToNext() {
                       processBA.raiseEventFromUI(getObject(), "media_onskiptonext", null);
                       return;  
   }
  
   public void onSkipToPrevious() {
                       processBA.raiseEventFromUI(getObject(), "media_onskiptoprevious", null);
                       return;  
   }        
}  
}
#end if

I tried making my classes static but get the same problem

Can any provide some help?


JUST so everyone knows, I have looked at all the examples I can find online that do this. And I believe my classes and code is right. WELL other than the code that is trying to create an instance of the class. Just don't know how to do this in B4A



BobVal
 
Last edited:

Robert Valentino

Well-Known Member
Licensed User
Longtime User
UPDATE:

change my classes to be static and then called instance them using static

B4X:
                  Private MediaBrowserService   As JavaObject  '  Is in Process_Globals

                   MediaBrowserService.InitializeStatic("android.support.v4.media.MediaBrowserServiceCompat")

                   Log("MyMediaBrowserService Initialize")

                   Dim CallBack    As JavaObject                     ' Is Local

                   CallBack.InitializeStatic(Application.PackageName & ".mymediabrowserservice.MyMediaBrowserService")               

                   Log("MyMediaBrowserService CallBack Initialize")

Now this code APPEARS to have worked... BUT my class constructor is not getting called
B4X:
public MyMediaBrowserService

Need to take a break been at this for two days.
 
Upvote 0
Top