强制停止android应用程序

时间:2016-12-27 15:25:00

标签: android

enter image description here使用后打开application details settings intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS),如何以编程方式强制停止应用程序?

4 个答案:

答案 0 :(得分:1)

您可以使用Accessibility来实现这一目标(但用户启用的应用需要Accessibility

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

    }
}

您可以在此示例中查看: AccessibilityTestService.java

答案 1 :(得分:0)

您有两种方式,更粗鲁更好

良好做法

如果您只有一项活动正在运行

this.finish();方法就足够了

如果您有多项活动正在运行

你必须调用this.finishAffinity();方法。 这是一般情况下的最佳做法,您可以同时进行单项或多项活动

粗鲁的方式

System.Exit(0);

我仅在信息中添加了此信息,但这可能不适用于多个活动,这不是关闭应用的好方法。它主要类似于&#34; 按住电源按钮,直到电脑关闭&#34;。

答案 2 :(得分:0)

在运行时单击另一个应用程序的元素会被视为安全威胁。你需要一个黑客来克服这个障碍。

我最近发现了一个黑客,你可以利用它。您可以在此处找到源代码:https://github.com/tfKamran/android-ui-automator

您可以在此处将代码作为模块添加到应用中,并使用操作com.tf.uiautomator.ACTION_CLICK_ITEM调用服务,并使用键itemText发送要单击的元素的文本作为额外内容。

您可以使用adb来测试它:

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

答案 3 :(得分:0)

我找到了一种强制停止的解决方案。强制停止后,我怎样才能回到我的活动页面?

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);
}

}