adb shell am 命令启动activity

adb shell am start-activity -n com.chaozh.iReader/com.zhangyue.iReader.read.ui.Activity_BookBrowser_TXT -e FilePath /storage/emulated/0/abc.txt

-n 表示用名称启动,/ 前面是包名,后面是全类名。否则就会启动失败。

我们看下源码:

android.content.Intent#parseCommandArgs


 public static Intent parseCommandArgs(ShellCommand cmd, CommandOptionHandler optionHandler)
            throws URISyntaxException {
        Intent intent = new Intent();
        Intent baseIntent = intent;
        boolean hasIntentInfo = false;

        Uri data = null;
        String type = null;

        String opt;
        while ((opt=cmd.getNextOption()) != null) {
        。。。。。
		case "-n": {
                    String str = cmd.getNextArgRequired();
                    ComponentName cn = ComponentName.unflattenFromString(str);
                    if (cn == null)
                        throw new IllegalArgumentException("Bad component name: " + str);
                    intent.setComponent(cn);
                    if (intent == baseIntent) {
                        hasIntentInfo = true;
                    }
                }
                break;
android.content.ComponentName#unflattenFromString

    /**
     * Recover a ComponentName from a String that was previously created with
     * {@link #flattenToString()}.  It splits the string at the first '/',
     * taking the part before as the package name and the part after as the
     * class name.  As a special convenience (to use, for example, when
     * parsing component names on the command line), if the '/' is immediately
     * followed by a '.' then the final class name will be the concatenation
     * of the package name with the string following the '/'.  Thus
     * "com.foo/.Blah" becomes package="com.foo" class="com.foo.Blah".
     *
     * @param str The String that was returned by flattenToString().
     * @return Returns a new ComponentName containing the package and class
     * names that were encoded in str
     *
     * @see #flattenToString()
     */
    public static @Nullable ComponentName unflattenFromString(@NonNull String str) {
        int sep = str.indexOf('/');
        if (sep < 0 || (sep+1) >= str.length()) {
            return null;
        }
        String pkg = str.substring(0, sep);
        String cls = str.substring(sep+1);
        if (cls.length() > 0 && cls.charAt(0) == '.') {
            cls = pkg + cls;
        }
        return new ComponentName(pkg, cls);
    }
adb shell input keyevent "KEYCODE_BACK"

表示执行返回键

一个供参考的脚本:

# coding=utf-8
import os

import time

if __name__ == '__main__':
    print "aaa"
    while True:
        # os.system('adb shell am start-activity -n com.chaozh.iReader/com.zhangyue.iReader.read.ui.Activity_BookBrowser_TXT -e FilePath /storage/emulated/0/iReader/books/zyepub/11489147/《终极奇才》.zyepub')
        os.system('adb shell am start-activity -n com.chaozh.iReader/com.zhangyue.iReader.read.ui.Activity_BookBrowser_TXT -e FilePath /storage/emulated/0/abc.txt')
        # os.system('adb shell am start-activity -n com.chaozh.iReader/com.zhangyue.iReader.read.ui.Activity_BookBrowser_TXT -e FilePath /storage/emulated/0/iReader/books/zyepub/11853797/《娇妻在上,大叔求放过》.zyepub')
        # os.system('adb shell input keyevent "KEYCODE_BACK"')
        # count = 0;
        # while count < 5:
        #     os.system('adb shell input tap 1070 1900')
        #     count = count + 1;
        #     time.sleep(1)



        time.sleep(5)
        os.system('adb shell input keyevent "KEYCODE_BACK"')
        time.sleep(5)

你可能感兴趣的:(Android)