-
public class Shizuku
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceShizuku.OnBinderReceivedListenerpublic interfaceShizuku.OnBinderDeadListenerpublic interfaceShizuku.OnRequestPermissionResultListenerpublic classShizuku.UserServiceArgs
-
Field Summary
Fields Modifier and Type Field Description private static IBinderbinderprivate static intserverPatchVersion
-
Method Summary
Modifier and Type Method Description static IBindergetBinder()Get the binder. static intgetServerPatchVersion()static voidonBinderReceived(@Nullable() IBinder newBinder, String packageName)static voidaddBinderReceivedListener(@NonNull() Shizuku.OnBinderReceivedListener listener)Add a listener that will be called when binder is received. static voidaddBinderReceivedListenerSticky(@NonNull() Shizuku.OnBinderReceivedListener listener)Same to addBinderReceivedListener but only call the listenerimmediately if the binder is already received. static booleanremoveBinderReceivedListener(@NonNull() Shizuku.OnBinderReceivedListener listener)Remove the listener added by addBinderReceivedListener or addBinderReceivedListenerSticky. static voidaddBinderDeadListener(@NonNull() Shizuku.OnBinderDeadListener listener)Add a listener that will be called when binder is dead. static booleanremoveBinderDeadListener(@NonNull() Shizuku.OnBinderDeadListener listener)Remove the listener added by addBinderDeadListener. static voidaddRequestPermissionResultListener(@NonNull() Shizuku.OnRequestPermissionResultListener listener)Add a listener to receive the result of requestPermission. static booleanremoveRequestPermissionResultListener(@NonNull() Shizuku.OnRequestPermissionResultListener listener)Remove the listener added by addRequestPermissionResultListener. static booleanpingBinder()Check if the binder is alive. static voidtransactRemote(@NonNull() Parcel data, @Nullable() Parcel reply, int flags)Call transact at remote service. static ShizukuRemoteProcessnewProcess(@NonNull() Array<String> cmd, @Nullable() Array<String> env, @Nullable() String dir)Start a new process at remote service, parameters are passed to exec. static intgetUid()Returns uid of remote service. static intgetVersion()Returns remote service version. static booleanisPreV11()Returns if the remote service version belows 11. static intgetLatestServiceVersion()Return latest service version when this library was released. static StringgetSELinuxContext()Returns SELinux context of Shizuku server process. static voidbindUserService(@NonNull() Shizuku.UserServiceArgs args, @NonNull() ServiceConnection conn)User Service is similar to Bound Services.The difference is that the service runs in a different process and asthe identity (Linux UID) of root (UID 0) or shell (UID 2000, if thebackend is Shizuku and user starts Shizuku with adb). static intpeekUserService(@NonNull() Shizuku.UserServiceArgs args, @NonNull() ServiceConnection conn)Similar to bindUserService,but does not start user service if it is not running. static voidunbindUserService(@NonNull() Shizuku.UserServiceArgs args, @Nullable() ServiceConnection conn, boolean remove)Remove user service. static intcheckRemotePermission(String permission)Check if remote service has specific permission. static voidrequestPermission(int requestCode)Request permission. static intcheckSelfPermission()Check if self has permission. static booleanshouldShowRequestPermissionRationale()Should show UI with rationale before requesting the permission. static voidexit()static voidattachUserService(@NonNull() IBinder binder, @NonNull() Bundle options)static voiddispatchPermissionConfirmationResult(int requestUid, int requestPid, int requestCode, @NonNull() Bundle data)static intgetFlagsForUid(int uid, int mask)static voidupdateFlagsForUid(int uid, int mask, int value)-
-
Method Detail
-
getBinder
@Nullable() static IBinder getBinder()
Get the binder.
Normal apps should not use this method.
-
getServerPatchVersion
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static int getServerPatchVersion()
-
onBinderReceived
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static void onBinderReceived(@Nullable() IBinder newBinder, String packageName)
-
addBinderReceivedListener
static void addBinderReceivedListener(@NonNull() Shizuku.OnBinderReceivedListener listener)
Add a listener that will be called when binder is received.
Shizuku APIs can only be used when the binder is received, or a IllegalStateException will be thrown.
Note:
- The listener will be called in main thread.
- The listener could be called multiply times. For example, user restarts Shizuku when app is running.
- Parameters:
listener- OnBinderReceivedListener
-
addBinderReceivedListenerSticky
static void addBinderReceivedListenerSticky(@NonNull() Shizuku.OnBinderReceivedListener listener)
Same to addBinderReceivedListener but only call the listenerimmediately if the binder is already received.
- Parameters:
listener- OnBinderReceivedListener
-
removeBinderReceivedListener
static boolean removeBinderReceivedListener(@NonNull() Shizuku.OnBinderReceivedListener listener)
Remove the listener added by addBinderReceivedListener or addBinderReceivedListenerSticky.
- Parameters:
listener- OnBinderReceivedListener
-
addBinderDeadListener
static void addBinderDeadListener(@NonNull() Shizuku.OnBinderDeadListener listener)
Add a listener that will be called when binder is dead.
Note:
- The listener will be called in main thread.
- Parameters:
listener- OnBinderReceivedListener
-
removeBinderDeadListener
static boolean removeBinderDeadListener(@NonNull() Shizuku.OnBinderDeadListener listener)
Remove the listener added by addBinderDeadListener.
- Parameters:
listener- OnBinderDeadListener
-
addRequestPermissionResultListener
static void addRequestPermissionResultListener(@NonNull() Shizuku.OnRequestPermissionResultListener listener)
Add a listener to receive the result of requestPermission.
Note:
- The listener will be called in main thread.
- Parameters:
listener- OnBinderReceivedListener
-
removeRequestPermissionResultListener
static boolean removeRequestPermissionResultListener(@NonNull() Shizuku.OnRequestPermissionResultListener listener)
Remove the listener added by addRequestPermissionResultListener.
- Parameters:
listener- OnRequestPermissionResultListener
-
pingBinder
static boolean pingBinder()
Check if the binder is alive.
Normal apps should use listeners rather calling this method everytime.
-
transactRemote
static void transactRemote(@NonNull() Parcel data, @Nullable() Parcel reply, int flags)
Call transact at remote service.
Use ShizukuBinderWrapper to wrap the original binder.
-
newProcess
static ShizukuRemoteProcess newProcess(@NonNull() Array<String> cmd, @Nullable() Array<String> env, @Nullable() String dir)
Start a new process at remote service, parameters are passed to exec.From version 11, like "su", the process will be killed when the caller process is dead. If you have complicatedrequirements, use bindUserService.
Note, you may need to read/write streams from RemoteProcess in different threads.
-
getUid
static int getUid()
Returns uid of remote service.
-
getVersion
static int getVersion()
Returns remote service version.
-
isPreV11
static boolean isPreV11()
Returns if the remote service version belows 11.
-
getLatestServiceVersion
static int getLatestServiceVersion()
Return latest service version when this library was released.
-
getSELinuxContext
static String getSELinuxContext()
Returns SELinux context of Shizuku server process.
For adb, context should always be
u:r:shell:s0.For root, context depends on su the user uses. E.g., context of Magisk isu:r:magisk:s0.If the user's su does not allow binder calls between su and app, Shizuku will switch to contextu:r:shell:s0.
-
bindUserService
static void bindUserService(@NonNull() Shizuku.UserServiceArgs args, @NonNull() ServiceConnection conn)
User Service is similar to Bound Services.The difference is that the service runs in a different process and asthe identity (Linux UID) of root (UID 0) or shell (UID 2000, if thebackend is Shizuku and user starts Shizuku with adb).
The user service can run under "Daemon mode".Under "Daemon mode" (default behavior), the service will run foreveruntil you call the "unbind" method. Under "Non-daemon mode", the servicewill be stopped when the process which called the "bind" method is dead.
When the "unbind" method is called, the user service will NOT be killed.You need to implement a "destroy" method in your service. The transactioncode for that method is
{@code 16777115}(use{@code 16777114}in aidl).In this method, you can do some cleanup jobs and call exit in the end.If the backend is Shizuku, whether in daemon mode or not, user servicewill be killed when Shizuku service is stopped or restarted.Shizuku sends binder to all Shizuku apps. Therefore, you only need tostart the user service again.
Use Android APIs in user service:
There is no restrictions on non-SDK APIs in user service process.However, it is not an valid Android application process. Therefore,even you can acquire an
{@code Context}instance, many APIs, such as{@code Context#registerReceiver}and{@code Context#getContentResolver}will not work. You will need to dig into Android source code to findout how things works, so that you will be able to implement your servicesafely and elegantly.
-
peekUserService
static int peekUserService(@NonNull() Shizuku.UserServiceArgs args, @NonNull() ServiceConnection conn)
Similar to bindUserService,but does not start user service if it is not running.
-
unbindUserService
static void unbindUserService(@NonNull() Shizuku.UserServiceArgs args, @Nullable() ServiceConnection conn, boolean remove)
Remove user service.
You need to implement a "destroy" method in your service,or the service will not be killed.
- Parameters:
remove- Remove (kill) the remote user service.
-
checkRemotePermission
static int checkRemotePermission(String permission)
Check if remote service has specific permission.
- Parameters:
permission- permission name
-
requestPermission
static void requestPermission(int requestCode)
Request permission.
Different from runtime permission, you need to add a listener to receivethe result.
- Parameters:
requestCode- Application specific request code to match with a resultreported to onRequestPermissionResult.
-
checkSelfPermission
static int checkSelfPermission()
Check if self has permission.
-
shouldShowRequestPermissionRationale
static boolean shouldShowRequestPermissionRationale()
Should show UI with rationale before requesting the permission.
-
exit
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static void exit()
-
attachUserService
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static void attachUserService(@NonNull() IBinder binder, @NonNull() Bundle options)
-
dispatchPermissionConfirmationResult
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static void dispatchPermissionConfirmationResult(int requestUid, int requestPid, int requestCode, @NonNull() Bundle data)
-
getFlagsForUid
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static int getFlagsForUid(int uid, int mask)
-
updateFlagsForUid
@RestrictTo(value = LIBRARY_GROUP_PREFIX) static void updateFlagsForUid(int uid, int mask, int value)
-
-
-
-