Android Question Reflection - Access internal class

avacondios

Active Member
Licensed User
Longtime User
hi,

I am trying to get access on android.app.StatusBarManager class which it is not exposed on public API. The constructor of this class is the following :

B4X:
StatusBarManager(Context context) {
mContext = context;
}

I found it on https://github.com/android/platform...r/core/java/android/app/StatusBarManager.java.

I am trying to use the reflection object to get access on that class with the following code:

B4X:
Dim mRef As Reflector

mRef.Target = mRef.CreateObject2("android.app.StatusBarManager", Array As Object(mRef.GetContext), Array As String("android.content.Context") )

but I am getting the following exception :

B4X:
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.content.Context]
    at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject2(Reflection.java:268)
    at b4a.example.main._button1_click(main.java:416)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:162)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
    at android.view.View.performClick(View.java:4443)
    at android.view.View$PerformClick.run(View.java:18443)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context]
    at java.lang.Class.getConstructorOrMethod(Class.java:472)
    at java.lang.Class.getConstructor(Class.java:446)
    at anywheresoftware.b4a.agraham.reflection.Reflection.CreateObject2(Reflection.java:264)
    ... 18 more


My target was to open and close the notifications from the statusbar, using a button. I found the following code on http://stackoverflow.com/questions/...matically-open-close-notifications-in-android

B4X:
Object sbservice = getSystemService("statusbar");
Class<?> statusbarManager =Class.forName("android.app.StatusBarManager");
Method showsb;if(Build.VERSION.SDK_INT >=17){
showsb = statusbarManager.getMethod("expandNotificationsPanel");}
else
{showsb = statusbarManager.getMethod("expand");}
showsb.invoke( sbservice );

Any help please ?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The correct code is:
B4X:
Sub ExpandStatusBar
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "statusbar", "java.lang.String")
   Dim p As Phone
   Dim methodName As String
   If p.SdkVersion >= 17 Then methodName = "expandNotificationsPanel" Else methodName = "expand"
   r.RunMethod(methodName)
End Sub

And add this manifest code:
B4X:
AddPermission(android.permission.EXPAND_STATUS_BAR)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…