Is it possible to detect if an app is launched from adb? - android

I develop an android app and I want to execute a certain function if the app is launched from adb.
When the app is launched from the device this function will not be executed.
UPDATE
I use the below code
if ((Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
result = "ok";
return new PluginResult(status, result);
}
if ((Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
PluginResult.Status status = PluginResult.Status.ERROR;
String result = "";
result = "error";
return new PluginResult(status, result);
}
but i receive always ok.
Where am I wrong?

App launch from OS:
First thing is that if you launch the app from the launcher icon, the Intent includes the CATEGORY "android.intent.category.LAUNCHER".
When you launch via the launcher icon, the Intent flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (0x200000) is set.
App Lauch from ADB:
The adb shell case it is not.(adb shell am start -n com.package.name/com.package.name.ActivityName)
In the adb shell case FLAG_ACTIVITY_RESET_TASK_IF_NEEDED is not.
This will solve your question.all the best

Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED is a bitmask and a constant. The proper way to check if the flag is set is:
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
...

Related

How can I stop a system service programmatically on a rooted android phone?

I am developing an androd bluetooth telnet(?) server which gets commands via bluetooth OPP. My plan is to monitor incoming Opp push, check if it is from certain user, then starting a worker service which actually performs given work.
So I researched information about receiving bluetooth incoming OPP, and I found that killing BluetoothOppService is a key point in this SO thread.
So I wrote the codes below to accept incoming OPP push.
private void KillOriginalService()
{
java.lang.Process suProcess=null;
int pid;
try
{
String[] command = {
"/system/bin/sh",
"-c",
"ps|grep com.android.bl"
};
suProcess = Runtime.getRuntime().exec(command);
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes)
{
String line;
while (osRes.available() > 0)
{
line = osRes.readLine();
if (line.contains("1002"))
{
String[] words=line.split(" ");
//pid=Integer.parseInt(words[0]);
final String p=words[0];
new ExecuteAsRootBase(){
#Override
protected ArrayList<String> getCommandsToExecute()
{
// TODO: Implement this method
ArrayList<String> list=new ArrayList<String>();
list.add("system/bin/kill -9 " + p);
return list;
}
}.execute();
Log.v(TAG,"Success kill");
return;
}
}
}
}
catch (IOException e)
{
Log.e(TAG, "error occured trying to kill opp service ",e);
}
}
And the following code to get ServerSocket.
private void getServerSocket()
{
boolean fail = true;
while (fail)
{
try
{
serversocket = null;
Log.v(TAG, "trying to get serverSocket");
serversocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("OPP Listener", UUID.fromString("00001105-0000-1000-8000-00805f9b34fb"));
if(serversocket!=null)
fail = false;
}
catch (IOException e)
{
fail = true;
Log.e(TAG, "Failed to get serversocket " , e);
}
if (fail)
{
KillOriginalService();
}
}
//return serversocket;
}
And this code works but sometimes continually ignore the incomming connection until I restart my service manually, causing the original service to accept the connection, rejecting it (because the incoming file's mime type is null). Also the original service acquires full wakelock, consuming my device's battery significantly. Also even when mine accepts the connection, I have to fail about 2 times before mine accepts the connection instead of the original service.
I read the logcat outputs, and found out that the original BtOppService restarts after I kill it printing OnStartCommand logcat.
I solved the battery consuming problem by Administration screenoff api. But the first problem is not solved.
How can I make my service, not the original service, to receive every incoming connections?
(I am currently solving this problem by using watchdog thread that restarts my service automatically.)
P.S. I have a rooted device and the su works properly.
I finally found a way to stop the system services programmatically: Hook the system service!
As I had a rooted device, I could install XPosed Framework and modules. I can create a module that attatches to the target service, and then returning not START_STICKY can prevent it from being restarted.
However, it turned out to be a XY problem. I finally changed my implenentation to not Killing the system service, but Living with it.
I created an file observer to check if the system has received a file. When a file is received, it started a corresponding service.
It creates synergy effect with Auto-Accept, which makes a phone to accept every bluetooth OPP file transfer requests without having to ask the user(No prompts!).

The setMobileDataEnabled method is no longer callable as of Android L and later

I have logged Issue 78084 with Google regarding the setMobileDataEnabled() method being no longer callable via reflection. It was callable since Android 2.1 (API 7) to Android 4.4 (API 19) via reflection, but as of Android L and later, even with root, the setMobileDataEnabled() method is not callable.
The official response is that the issue is "Closed" and the status set to "WorkingAsIntended". Google's simple explanation is:
Private APIs are private because they are not stable and might disappear without notice.
Yes, Google, we are aware of the risk of using reflection to call hidden method- even before Android came on the scene- but you need to provide a more solid answer as to alternatives, if any, for accomplishing the same result as setMobileDataEnabled(). (If you are displeased with Google's decision as I am, then log into Issue 78084 and star it as many as possible to let Google know the error of their way.)
So, my question to you is: Are we at a dead end when it comes to programmatically enable or disable mobile network function on an Android device? This heavy-handed approach from Google somehow does not sit well with me. If you have workaround for Android 5.0 (Lollipop) and beyond, I would love to hear your answer/discussion in this thread.
I have used the code below to see if the setMobileDataEnabled() method is available:
final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
if (method.toGenericString().contains("set")) {
Log.i("TESTING", "Method: " + method.getName());
}
}
But it's not.
UPDATE: Currently, it's possible to toggle mobile network if the device is rooted. However, for non-rooted devices, it's still an investigative process as there is no universal method to toggle mobile network.
To extend Muzikant's Solution #2, can someone please try the solution below on an Android 5.0 rooted device (as I currently do not possess one) and let me know if it works or does not work.
To enable or disable mobile data, try:
// 1: Enable; 0: Disable
su -c settings put global mobile_data 1
su -c am broadcast -a android.intent.action.ANY_DATA_STATE --ez state 1
Note: The mobile_data variable can be found in Android API 21 source codes at /android-sdk/sources/android-21/android/provider/Settings.java and is declared as:
/**
* Whether mobile data connections are allowed by the user. See
* ConnectivityManager for more info.
* #hide
*/
public static final String MOBILE_DATA = "mobile_data";
While the android.intent.action.ANY_DATA_STATE Intent can be found in Android API 21 source codes at /android-sdk/sources/android-21/com/android/internal/telephony/TelephonyIntents.java and is declared as:
/**
* Broadcast Action: The data connection state has changed for any one of the
* phone's mobile data connections (eg, default, MMS or GPS specific connection).
*
* <p class="note">
* Requires the READ_PHONE_STATE permission.
* <p class="note">This is a protected intent that can only be sent by the system.
*
*/
public static final String ACTION_ANY_DATA_CONNECTION_STATE_CHANGED
= "android.intent.action.ANY_DATA_STATE";
UPDATE 1: If you don't want to implement the above Java codes in your Android application, then you can run the su commands via a shell (Linux) or command prompt (Windows) as follow:
adb shell "su -c 'settings put global mobile_data 1; am broadcast -a android.intent.action.ANY_DATA_STATE --ez state 1'"
Note: adb is located at /android-sdk/platform-tools/ directory. The settings command is only supported on Android 4.2 or later. Older Android version will report a "sh: settings: not found" error.
UPDATE 2: Another way to toggle mobile network on a rooted Android 5+ device would be to use the undocumented service shell command. The following command can be executed via ADB to toggle mobile network:
// 1: Enable; 0: Disable
adb shell "su -c 'service call phone 83 i32 1'"
Or just:
// 1: Enable; 0: Disable
adb shell service call phone 83 i32 1
Note 1: The transaction code 83 used in the service call phone command might change between Android versions. Please check com.android.internal.telephony.ITelephony for the value of the TRANSACTION_setDataEnabled field for your version of Android. Also, instead of hardcoding 83, you would be better off using Reflection to get the value of the TRANSACTION_setDataEnabled field. This way, it will work across all mobile brands running Android 5+ (If you don't know how to use Reflection to get the value of the TRANSACTION_setDataEnabled field, see solution from PhongLe below- save me from duplicating it here.) Important: Please note that transaction code TRANSACTION_setDataEnabled has only been introduced in Android 5.0 and later versions. Running this transaction code on earlier versions of Android will do nothing as the transaction code TRANSACTION_setDataEnabled does not exist.
Note 2: adb is located at /android-sdk/platform-tools/ directory. If you do not wish to use ADB, execute the method via su in your app.
Note 3: See UPDATE 3 below.
UPDATE 3: Many Android developers have emailed me questions regarding switching mobile network on/off for Android 5+, but instead of answering individual emails, I'll post my answer here so everyone can use it and adapt it for their Android apps.
First thing first, let's clear up some misconception and misunderstanding regarding:
svc data enable
svc data disable
The above methods would only turn background data on/off, not the subscription service, so the battery will drain a fair bit since the subscription service- an Android system service- will still be running in the background. For Android devices supporting multiple sim cards, this scenario is worse as the subscription service constantly scans for available mobile network(s) to use with the active SIM cards available in the Android device. Use this method at your own risk.
Now, the proper way to switch off mobile network, including its corresponding subscription service via the SubscriptionManager class introduced in API 22, is:
public static void setMobileNetworkfromLollipop(Context context) throws Exception {
String command = null;
int state = 0;
try {
// Get the current state of the mobile network.
state = isMobileDataEnabledFromLollipop(context) ? 0 : 1;
// Get the value of the "TRANSACTION_setDataEnabled" field.
String transactionCode = getTransactionCode(context);
// Android 5.1+ (API 22) and later.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
// Loop through the subscription list i.e. SIM list.
for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) {
if (transactionCode != null && transactionCode.length() > 0) {
// Get the active subscription ID for a given SIM card.
int subscriptionId = mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId();
// Execute the command via `su` to turn off
// mobile network for a subscription service.
command = "service call phone " + transactionCode + " i32 " + subscriptionId + " i32 " + state;
executeCommandViaSu(context, "-c", command);
}
}
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
// Android 5.0 (API 21) only.
if (transactionCode != null && transactionCode.length() > 0) {
// Execute the command via `su` to turn off mobile network.
command = "service call phone " + transactionCode + " i32 " + state;
executeCommandViaSu(context, "-c", command);
}
}
} catch(Exception e) {
// Oops! Something went wrong, so we throw the exception here.
throw e;
}
}
To check if the mobile network is enabled or not:
private static boolean isMobileDataEnabledFromLollipop(Context context) {
boolean state = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
state = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
}
return state;
}
To get the value of the TRANSACTION_setDataEnabled field (borrowed from PhongLe's solution below):
private static String getTransactionCode(Context context) throws Exception {
try {
final TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final Class<?> mTelephonyClass = Class.forName(mTelephonyManager.getClass().getName());
final Method mTelephonyMethod = mTelephonyClass.getDeclaredMethod("getITelephony");
mTelephonyMethod.setAccessible(true);
final Object mTelephonyStub = mTelephonyMethod.invoke(mTelephonyManager);
final Class<?> mTelephonyStubClass = Class.forName(mTelephonyStub.getClass().getName());
final Class<?> mClass = mTelephonyStubClass.getDeclaringClass();
final Field field = mClass.getDeclaredField("TRANSACTION_setDataEnabled");
field.setAccessible(true);
return String.valueOf(field.getInt(null));
} catch (Exception e) {
// The "TRANSACTION_setDataEnabled" field is not available,
// or named differently in the current API level, so we throw
// an exception and inform users that the method is not available.
throw e;
}
}
To execute command via su:
private static void executeCommandViaSu(Context context, String option, String command) {
boolean success = false;
String su = "su";
for (int i=0; i < 3; i++) {
// Default "su" command executed successfully, then quit.
if (success) {
break;
}
// Else, execute other "su" commands.
if (i == 1) {
su = "/system/xbin/su";
} else if (i == 2) {
su = "/system/bin/su";
}
try {
// Execute command as "su".
Runtime.getRuntime().exec(new String[]{su, option, command});
} catch (IOException e) {
success = false;
// Oops! Cannot execute `su` for some reason.
// Log error here.
} finally {
success = true;
}
}
}
Hope this update clears up any misconception, misunderstanding, or question you may have about switching mobile network on/off on rooted Android 5+ devices.
Just to share a few more insights and possible solution (for rooted devices and system apps).
Solution #1
It seems like the setMobileDataEnabled method no longer exists in ConnectivityManager and this functionality was moved to TelephonyManager with two methods getDataEnabled and setDataEnabled.
I tried calling these methods with reflection as you can see in the code below:
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
When executing the code you get a SecurityException stating that Neither user 10089 nor current process has android.permission.MODIFY_PHONE_STATE.
So, yes this is an intended change to the internal API and is no longer available to apps that used that hack in previous versions.
(start rant: that dreadful android.permission.MODIFY_PHONE_STATE permission... end rant).
The good news are that in case you are building an app that can acquire the MODIFY_PHONE_STATE permission (only system apps can use that), you can use the above code to toggle mobile data state.
Solution #2
To check for current state of mobile data you can use the mobile_data field of Settings.Global (not documented in official documentation).
Settings.Global.getInt(contentResolver, "mobile_data");
And to enable/disable mobile data you can use shell commands on rooted devices (Just basic testing performed so any feedback in comments is appreciated).
You can run the following command(s) as root (1=enable, 0=disable):
settings put global mobile_data 1
settings put global mobile_data 0
I noticed that the service call method posted by ChuongPham does not work consistently on all devices.
I have found the following solution which, I think, will work without any issue on all ROOTED devices.
Execute the following via su
To enable mobile data
svc data enable
To disable mobile data
svc data disable
I think this is the simplest and best method.
Edit:
2 downvotes were for what I believe to be commercial reasons. The person has deleted his comment now. Try it yourself, it works!
Also confirmed to work by guys in comments.
I don't have enough reputation to comment but I have tried all the answers and found the following:
ChuongPham: Instead of using 83, I used reflection to get the value of the variable TRANSACTION_setDataEnabled from the com.android.internal.telephony.ITelephony so it works across all Android 5+ devices, regardless of brands.
Muzikant: Work if the app is moved to /system/priv-app/ directory (thanks to rgruet.) Else, it works via root, too! You just need to inform your users that the app will need a reboot before the changes to the mobile network will take place.
AJ: Work- sort of. Does not turn off subscription service so the devices I tested drained their batteries a fair bit. AJ's solution is NOT equivalent to Muzikant's solution despite the claim. I can confirm this by debugging different Samsung, Sony, and LG stock ROMs (I'm thorough) and can disprove AJ's claim that his solution is the same as Muzikant's. (Note: I can't get my hands on some Nexus and Motorola ROMs so haven't tested these ROMs with the proposed solutions.)
Anyway, hope it clears up any doubt over the solutions.
Happy coding!
PL, Germany
UPDATE: For those wondering how to get the value of the TRANSACTION_setDataEnabled field via reflection, you can do the following:
private static String getTransactionCodeFromApi20(Context context) throws Exception {
try {
final TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final Class<?> mTelephonyClass = Class.forName(mTelephonyManager.getClass().getName());
final Method mTelephonyMethod = mTelephonyClass.getDeclaredMethod("getITelephony");
mTelephonyMethod.setAccessible(true);
final Object mTelephonyStub = mTelephonyMethod.invoke(mTelephonyManager);
final Class<?> mTelephonyStubClass = Class.forName(mTelephonyStub.getClass().getName());
final Class<?> mClass = mTelephonyStubClass.getDeclaringClass();
final Field field = mClass.getDeclaredField("TRANSACTION_setDataEnabled");
field.setAccessible(true);
return String.valueOf(field.getInt(null));
} catch (Exception e) {
// The "TRANSACTION_setDataEnabled" field is not available,
// or named differently in the current API level, so we throw
// an exception and inform users that the method is not available.
throw e;
}
}
I found that su -c 'service call phone 83 i32 1' solution is most reliable for rooted devices. Thanks to Phong Le reference I have improved it by getting vendor/os specific transaction code using reflection. Maybe it will be useful for someone else. So, here is source code:
public void changeConnection(boolean enable) {
try{
StringBuilder command = new StringBuilder();
command.append("su -c ");
command.append("service call phone ");
command.append(getTransactionCode() + " ");
if (Build.VERSION.SDK_INT >= 22) {
SubscriptionManager manager = SubscriptionManager.from(context);
int id = 0;
if (manager.getActiveSubscriptionInfoCount() > 0)
id = manager.getActiveSubscriptionInfoList().get(0).getSubscriptionId();
command.append("i32 ");
command.append(String.valueOf(id) + " ");
}
command.append("i32 ");
command.append(enable?"1":"0");
command.append("\n");
Runtime.getRuntime().exec(command.toString());
}catch(IOException e){
...
}
}
private String getTransactionCode() {
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
Object ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
Class ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
Class stub = ITelephonyClass.getDeclaringClass();
Field field = stub.getDeclaredField("TRANSACTION_setDataEnabled");
field.setAccessible(true);
return String.valueOf(field.getInt(null));
} catch (Exception e) {
if (Build.VERSION.SDK_INT >= 22)
return "86";
else if (Build.VERSION.SDK_INT == 21)
return "83";
}
return "";
}
Update:
Some of my users report that they have problem with turning ON mobile network via this method (turning off works correct). Does anyone have solution?
Update2:
After some digging the Android 5.1 code I've found that they changed signature of transaction. Android 5.1 brings official support of multi-SIM. So, the transaction need so-called Subscription Id as first parameter (read more here). The result of this situation is that the command su -c 'service call phone 83 i32 1' doesn't turn on Mobile Net on Android 5.1. So, the full command on Android 5.1 should be like this su -c 'service call phone 83 i32 0 i32 1' (the i32 0 is the subId, the i32 1 is command 0 - off and 1 - on). I've update the code above with this fix.
Solution #1 from Muzikant seems to work if you make the app "system" by moving the .apk to the /system/priv-app/ folder, not to the /system/app/ one (#jaumard: maybe that's why your test didn't work).
When the .apk is in the /system/priv-app/ folder, it can successfully request the dreadful android.permission.MODIFY_PHONE_STATE permission in the Manifest and call TelephonyManager.setDataEnabled and TelephonyManager.getDataEnabled.
At least that works on Nexus 5/ Android 5.0. The .apk perms are 0144. You need to reboot the device for the change to be taken into account, maybe this could be avoided - see this thread.
I derived final code from #ChuongPham and #A.J. for enable and disable cellular data. for enable you can call setMobileDataEnabled(true); and for disable you can call setMobileDataEnabled(false);
public void setMobileDataEnabled(boolean enableOrDisable) throws Exception {
String command = null;
if (enableOrDisable) {
command = "svc data enable";
} else {
command = "svc data disable";
}
executeCommandViaSu(mContext, "-c", command);
}
private static void executeCommandViaSu(Context context, String option, String command) {
boolean success = false;
String su = "su";
for (int i = 0; i < 3; i++) {
// Default "su" command executed successfully, then quit.
if (success) {
break;
}
// Else, execute other "su" commands.
if (i == 1) {
su = "/system/xbin/su";
} else if (i == 2) {
su = "/system/bin/su";
}
try {
// Execute command as "su".
Runtime.getRuntime().exec(new String[]{su, option, command});
} catch (IOException e) {
success = false;
// Oops! Cannot execute `su` for some reason.
// Log error here.
} finally {
success = true;
}
}
}
Not all phones and versions of android have things Enable/disable mobile data the same.
otherwise, this solution is tested on my phone (SAMSUNG SM-J100H)
To enable mobile data :
adb shell service call phone 27
To disable mobile data :
adb shell service call phone 28
To correct Muzikant Solution #2
settings put global mobile_data 1
Does enable only the toggle for mobile data but does nothing to the connectivity. Only the toggle is enabled. In order to get the data working using
su -c am broadcast -a android.intent.action.ANY_DATA_STATE --ez state 1
Gives error as the extra for
android.intent.action.ANY_DATA_STATE
Requires String Object while --ez parameter is used for boolean. Ref: PhoneGlobals.java & PhoneConstants.java. After using connecting or connected as extra using command
su -c am broadcast -a android.intent.action.ANY_DATA_STATE --es state connecting
Still doesnt do anything to enable the data.
The following solution works by enabling and disabling mobile data (as you would by clicking on the slider "Mobile data"). Requires root. Tested on LineageOS 16.0 (rooted):
Steps 1-3 are taken from the accepted answer at Turning off second SIM via adb shell/Tasker - using activities/intents:
Download jadx from https://github.com/skylot/jadx
ADB pull the devices framework.jar (adb pull /system/framework/framework.jar)
Open the .jar file with 7-Zip and extract the *.dex files. Open each .dex file with jadx-gui until you find the one with the following tree: com.android.internal.telephony.ITelephony
Find TRANSACTION_enableDataConnectivity and TRANSACTION_disableDataConnectivity, for me these are 38 and 39 respectively
From a root shell (e.g., adb shell or Termux), run service call phone 38 to enable data, and service call phone 39 to disable data.

Starting application in another Android smartphone over bluetooth

As the title suggests, I am developing an app, which can connect and pair with another Android phone. I can successfully scan, connect, pair and transmit String data over bluetooth. I tested by sending String data and received it in another android smartphone and showed it in the toast.
The actual scenario
This application will be installed in both android phones. Once another android phone scans and connect to the phone over bluetooth, then I want to launch this app. I know we can accomplish this using BroadcastReceiver. But how can I implement this in terms of Bluetooth connection. Any code snippet or idea or suggestions are welcomed..
After you get the BT msg,Hope you followed this link ,Here after "// Send the obtained bytes to the UI activity"
Send a broadcast to start your app.
Loop with the list of apps installed and know which app to be launched and use the below to launch a app.
public String getPreInstalledPocPkgName(){
String pkgName = null;
try {
List<PackageInfo> pkgInfoList = mContext.getPackageManager().getInstalledPackages(0);
for(PackageInfo pkgInfo:pkgInfoList){
pkgName = pkgInfo.packageName;
if(pkgName==null || pkgName.equals("")){
continue;
}
for(String name:pttPkgList){
if(pkgName.equals(name)){
Log.d(TAG, "------------ app with package name ["+pkgName+"] already installed----------");
return pkgName;
}
}
}
} catch (Exception e) {
Log.e(TAG, "-------Error! while fetching previously installed app------", e);
clearConfigFromSharedPath();
}
Log.d(TAG, "------------No previously installed app----------");
return null;
}
}
String pttAppPkgName = getPreInstalledPocPkgName();
Or
pttAppPkgName = your app package.
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(pttAppPkgName);
startActivity(intent);

How to freeze applications on rooted android device

How can I Freeze/Unfreeze applications on the rooted device like Titanium Backup app so that i make my app non-accessable from a rooted device. Any method to trace whether the device is rooted or not?
i am using RootTools library to do disable/enable command
CommandCapture command_enable = new CommandCapture(0,"pm enable "+ Package_Name);
RootTools.getShell(true).add(command_enable).waitForFinish();
CommandCapture command_disable = new CommandCapture(0,"pm disable "+ Package_Name);
RootTools.getShell(true).add(command_disable).waitForFinish();
You could actually try to run a root command and catch the response and determine if the device is rooted.
Taking code from here and modifying it a little to suit your needs, I think it could go like this:
public static boolean canRunRootCommands()
{
boolean retval = false;
Process suProcess;
try
{
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes)
{
retval = true;
/*
*if got to this point, its safe to assume the
*device is rooted and here you can do something
*to tell your app that su is present. Or you could
*use the bool return of this function to know that the
*device is rooted and make the app act different.
*/
}
}
catch (Exception e)
{
// Can't get root !
// [...] meaning that the device is not rooted
retval = false;
}
return retval;
}
I have not tested this but I'm sure it will help you. Or at least it will point you in the right way.

How to switch on wifi in uiautomator test case in android device?

I want to switch on wifi as a part of test case using uiautomator tool in android. I tried using following code in uiautomator test case:
WifiManager wi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(wi.isWifiEnabled()){
wi.setWifiEnabled(false);
}else{
wi.setWifiEnabled(true);
}
but it gave this error:
"getSystemservice" method is undefined for Mainclass
You can actually use UIAutomator to set the WiFi setting on and off. I wrote the code this evening :)
Here's the code. You can add it to the Android example which is here http://developer.android.com/tools/testing/testing_ui.html
Add the following enum at the top of the class
private enum OnOff {
Off,
On
};
Add the new code after:
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(new UiSelector()
.packageName("com.android.settings"));
assertTrue("Unable to detect Settings", settingsValidation.exists());
Here is the new code:
UiSelector settingsItems = new UiSelector().className(android.widget.TextView.class.getName());
UiObject wiFi = appViews.getChildByText(settingsItems, "Wi-Fi");
// We can click on Wi-Fi, e.g. wiFi.clickAndWaitForNewWindow();
// So we know we have found the Wi-Fi setting
UiSelector switchElement = new UiSelector().className(android.widget.Switch.class.getName());
setSwitchTo(OnOff.Off); // Or set it to On as you wish :)
}
private void setSwitchTo(OnOff value) throws UiObjectNotFoundException {
String text;
UiObject switchObject = getSwitchObject();
for (int attempts = 0; attempts < 5; attempts++) {
text = switchObject.getText();
boolean switchIsOn = switchObject.isChecked();
final OnOff result;
if (switchIsOn) {
result = OnOff.On;
} else {
result = OnOff.Off;
}
System.out.println("Value of switch is " + switchObject.isSelected() + ", " + text + ", " + switchIsOn);
if (result == value) {
System.out.println("Switch set to correct value " + result);
break;
} else {
switchObject.click();
}
}
}
private UiObject getSwitchObject() {
UiObject switchObject = new UiObject(new UiSelector().className(android.widget.Switch.class.getName()));
assertTrue("Unable to find the switch object", switchObject.exists());
String text;
return switchObject;
}
The loop was to compensate for some behaviour I observed, where the click didn't seem to change the switch position.
To enable WiFi:
device.executeShellCommand("svc wifi enable");
To disable WiFi:
device.executeShellCommand("svc wifi disable");
These are the commands to use on your UiDevice.
Used on production on Android 4.2 and 4.4
To open the Android Wifi settings in your code:
final Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
mContext.startActivity(intent);
To click the on/off switch with UiAutomator (after you're sure you're on the good activity):
public void enableWifiOnAndroidWifiSettings(boolean enabled) throws UiObjectNotFoundException {
final UiSelector wifiSwitchSelector = new UiSelector().className(android.widget.Switch.class.getName());
UiObject wifiSwitch = UiDevice.getInstance(sInstrumentation).findObject(wifiSwitchSelector);
if (wifiSwitch.waitForExists(5000) && wifiSwitch.isEnabled()) {
if (wifiSwitch.isChecked() != enabled) {
wifiSwitch.click();
}
}
}
Known limitation: It's searching the first Switch available. If you've custom ROM or if the Android Settings app evolves in the future, it will maybe not be enough.
In my suite tests with UIAutomator I use:
Runtime.getRuntime().exec("\"svc wifi disable\"")
Runtime.getRuntime().exec("\"svc wifi enable\"")
Runtime.getRuntime().exec("\"svc data disable\"")
Runtime.getRuntime().exec("\"svc data enable\"")
You can enable or disable wifi through adb as follows
adb shell sqlite3
/data/data/com.android.providers.settings/databases/settings.db update
secure set value=1 where name='wifi_on'; .exit
But you cannot use uiautomator tool to do the same
You can't do this. A UI Automator test doesn't run as part of the Android framework, so it has no access to Android system services. It's meant to test UIs; it doesn't claim to be a full-featured test framework. Turn on WiFi manually before you run the test.

Categories

Resources