Vishal Chaudhari
Vishal Chaudhari

Reputation: 393

Force stop android applications

enter image description hereAfter opening application details settings using intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS), how can I force stop application programmatically?

Upvotes: 0

Views: 1703

Answers (4)

Vishal Chaudhari
Vishal Chaudhari

Reputation: 393

I found one solution for force stop. After force stop how can i go back to my activity page ?

public class DeviceAccessibilityService extends AccessibilityService {

private static final String TAG = "litan";
private boolean isKilled = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    isKilled = false;
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
        AccessibilityNodeInfo nodeInfo = event.getSource();
        Log.i(TAG, "ACC::onAccessibilityEvent: nodeInfo=" + nodeInfo);
        if (nodeInfo == null) {
            return;
        }

        List<AccessibilityNodeInfo> list = new ArrayList<>();
        if ("com.android.settings.applications.InstalledAppDetailsTop".equals(event.getClassName())) {
            if (Build.VERSION.SDK_INT >= 18) {
                list = nodeInfo.findAccessibilityNodeInfosByViewId("com.android.settings:id/right_button");

            } else if (Build.VERSION.SDK_INT >= 14) {
                list = nodeInfo.findAccessibilityNodeInfosByText("com.android.settings:id/right_button");
            }
            for (AccessibilityNodeInfo node : list) {
                Log.i(TAG, "ACC::onAccessibilityEvent: left_button " + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        } else if ("android.app.AlertDialog".equals(event.getClassName())) {
            list = new ArrayList<>();
            if (Build.VERSION.SDK_INT >= 18) {
                list = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/button1");

            } else if (Build.VERSION.SDK_INT >= 14) {
                list = nodeInfo.findAccessibilityNodeInfosByText("android:id/button1");
            }

            for (final AccessibilityNodeInfo node : list) {
                Log.i(TAG, "ACC::onAccessibilityEvent: button1 " + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                //node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
        return;
    }
}


@Override
public void onInterrupt() {
    // TODO Auto-generated method stub
    Log.i("Interrupt", "Interrupt");
}

@Override
protected void onServiceConnected() {
    AccessibilityServiceInfo info = getServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOWS_CHANGED | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;

    info.flags = AccessibilityServiceInfo.DEFAULT;
    info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
    info.flags = AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS;
    info.flags = AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY;
    info.flags = AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
    // We are keeping the timeout to 0 as we don’t need any delay or to pause our accessibility events
    info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
    info.notificationTimeout = 100;
    this.setServiceInfo(info);
    // Toast.makeText(getApplicationContext(), "onServiceConnected", Toast.LENGTH_SHORT).show();
}

private static void logd(String msg) {
    Log.d(TAG, msg);
}

private static void logw(String msg) {
    Log.w(TAG, msg);
}

private static void logi(String msg) {
    Log.i(TAG, msg);
}

}

Upvotes: 0

Chris Maverick
Chris Maverick

Reputation: 928

You can use Accessibility to achieve that (but it needs Accessibility for your app turned on by user)

public class MyAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        //TYPE_WINDOW_STATE_CHANGED == 32
        if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event
            .getEventType()) {
        AccessibilityNodeInfo nodeInfo = event.getSource();

        if (nodeInfo == null) {
            return;
        }

        List<AccessibilityNodeInfo> list = nodeInfo
                  .findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button");
        //We can find button using button name or button id
        for (AccessibilityNodeInfo node : list) {
            node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }

        list = nodeInfo
                .findAccessibilityNodeInfosByViewId("android:id/button1");
        for (AccessibilityNodeInfo node : list) {
            node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
      }
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub

    }
}

You can check it out in this example: AccessibilityTestService.java

Upvotes: 2

Kamran Ahmed
Kamran Ahmed

Reputation: 7761

Clicking an element of another application on runtime is something that will be considered as a security threat. You would need a hack to go past this hurdle.

There is one hack that I recently found out, you can probably make use of it. You can find the source code here: https://github.com/tfKamran/android-ui-automator

You can add the code in here as a module in your app and invoke a service with action com.tf.uiautomator.ACTION_CLICK_ITEM and send the text of the element you want to click on as an extra with key itemText.

You can test it using adb like:

adb shell am startservice -a com.tf.uiautomator.ACTION_CLICK_ITEM -e itemText "OK"

Upvotes: 0

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

You have two ways, a more rude one and a better one

The good practice

If you have only one activity running

the this.finish(); method will be enough

If you have multiple activities running

You gotta call the this.finishAffinity(); method. This is the best practice in general cases, where you can have both a single or multiple activities

The rude way

System.Exit(0);

I added this only for info, but this might not work with multiple activities and this is not a good way for closing apps. It's mostly like the "Hold power button until the pc shuts down".

Upvotes: 0

Related Questions