Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 命令:實現啟動一個apk

Android 命令:實現啟動一個apk

編輯:關於Android編程

1、產生背景:

為了調試方便,常常希望通過知道apk的路徑,直接去啟動它,所以就寫了這個命令

2、使用方法:

adb push launch /data/

adb push launch.jar /data/

adb shell

launch

其實放到/system/下面也可以,把jar包放到/system/framework/下面,把launch命令放到/system/bin/下面,

然後adb shell 進去,直接執行launch就可以了,傳入apk的路徑參數。

3、eg:

launch /data/app/abc.apk

4、代碼:


Android.mk:

# Copyright 2008 The Android Open Source Project
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := launch 
include $(BUILD_JAVA_LIBRARY)


launch:

# Script to start "launch" on the device, which has a very rudimentary
# shell.
#

export CLASSPATH=/system/framework/launch.jar
exec app_process /system/bin com.android.commands.launch.Launch $*

#export CLASSPATH=/data/launch.jar
#exec app_process /data/ com.android.commands.launch.Launch $*

Launch.java:

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.commands.launch;

import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.IActivityController;
import android.app.IActivityManager;
import android.app.IInstrumentationWatcher;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.IIntentReceiver;
import android.content.Intent;
import android.content.pm.IPackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.util.AndroidException;
import android.view.Display;
import android.view.IWindowManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.List;
import android.util.Log;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageUserState;
import android.content.pm.PackageParser;
import android.util.DisplayMetrics;

/**
 * Launch  
 */

public class Launch {
    private static final String TAG = "Launch";
    private IPackageManager mPM;
    private IActivityManager mAm;

    /**
     * Command-line entry point.
     *
     * @param args The command-line arguments
     */
    public static void main(String[] args) {
    	
        (new Launch()).run(args);
    }
    private void init(){
	IPackageManager mPM = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
	mAm = ActivityManagerNative.getDefault();
    }

    private void run(String[] args) {
	init();
        if (args.length < 1) {
            showUsage();
            return;
        }

        String command = args[0];
	if(args.length == 1){
		PackageInfo info = getPackageArchiveInfo(command, PackageManager.GET_ACTIVITIES);
		if(info != null){
			ApplicationInfo appInfo = info.applicationInfo;
			String packageName = appInfo.packageName;
			String className = info.activities[0].name;
			log("-->packageName=" + packageName);
			log("-->className=" + className);
			log("-->all activities:");
			for(int i = 0; i < info.activities.length; i++){
				log(info.activities[i]);
			}
		
			startApp(packageName, className);
			//String execCmd= "am start -n " + packageName + "/" + className;
			//log(execCmd);
			//runLinuxCmd(execCmd);
		}
		return ;
	}
	System.err.println("Error: Invalid arguments for command: " + command);
        showUsage();
    }


    private PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
        PackageParser packageParser = new PackageParser(archiveFilePath);
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();
        final File sourceFile = new File(archiveFilePath);
        PackageParser.Package pkg = packageParser.parsePackage(
                sourceFile, archiveFilePath, metrics, 0);
        if (pkg == null) {
            return null;
        }
        if ((flags & PackageManager.GET_SIGNATURES) != 0) {
            packageParser.collectCertificates(pkg, 0);
        }
        PackageUserState state = new PackageUserState();
        return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, state);
    }

    private Process runLinuxCmd(String cmd){
		Process process = null;
		try{
			process = Runtime.getRuntime().exec(cmd);
			return process;
		} catch(IOException e){
			Log.d(TAG, "runLinuxCmd error:" +e);
		}
		return process;
    }
    private void startApp(String packageName, String className){
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		ComponentName comp = new ComponentName(packageName, className);
		intent.setComponent(comp);
		try{
			mAm.startActivityAndWait(null, intent, "",
								null, null, 0, 0, "", null, null, 0);
		}catch(RemoteException e){
			log("startApp error:"  + e);
		}
    }

    private void showUsage() {
        System.err.println("usage: input ...");
        System.err.println("       launch ");
    }
    private void log(Object o){
		System.err.println("" + o);
    }
}


放到源碼/framework/base/cmds/launch/目錄下面編譯就可以了!

親測,可以的,祝你好運!得意


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved