I have an application with a translucent activity destroyed with finish()
after starting a service that runs on the background and listens to USB_STATE
action then executes this command: service call connectivity CODE i32 1
when connected. very simple.
That's to enable USB Tethering (using root of course). But the real problem is CODE
since it differs from SDK to another. for example, if I typed service call connectivity 39 i32 1
, it'll only run on Samsung Nougat.
So, I thought of two different ways to overcome this
- 1st: Creating a
switch case
statement that switchesBuild.VERSION.SDK_INT
and according to the evaluated case, It'll set the service code from the list that I've taken from this answer...
Bug: That list is missing a lot of old & new service codes
Even if I've got them totally! (means all the cases of SDK_INT
& service CODEs
). Then what about incoming new versions of Android? Who wants to update such a simple App every android release!
- 2nd: A very bad way like iterating
i
from 30 to 100 replacingCODE
in the command above and executing it then what works, well... just works.
Bug: Bad coding style, consumes more power and the app may stop responding if fired a lot.
I just need my app to dynamically && programmatically get the service CODE
so how?
EDIT: I tried using reflection but it doesn't work either.
try {
// Also tried setUsbTethering
// and ConnectivityManager.setUsbTethering
Method method = this.getClass().getMethod("IConnectivityManager.setUsbTethering", Integer.class);
method.invoke(null, true);
Toast.makeText(this, "turned on", Toast.LENGTH_LONG).show();
}catch (Exception e)
{
Log.e("AUTO TETHERING", e.getMessage(), e);
}
But I get NoSuchMethodException