当前位置:首页 > 嵌入式培训 > 嵌入式学习 > 讲师博文 > APK之间的互相调用

APK之间的互相调用 时间:2018-09-21      来源:未知

APK之间的互相调用,主要的实现方法就是通过intent.setComponent(componentName),

Java Code

// "包名","activity名"activity名需包含完整的路径

componentName = new ComponentName("com.farsight.thisisthesecondone",

"com.farsight.thisisthesecondone.MainActivity");

Intent intent = new Intent();

intent.setComponent(componentName);

//告诉设备动作类型

intent.setAction("android.intent.action.MAIN");

//下边这句话是获取APP的特征值的

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//开始跳转

startActivity(intent);

在拥有两个APK的源码的情况下:

直接阅读两个源码的androidmanifest.xml文件,找到其中的头中的package=项,即为我们要调用的时候给ComponentName的构造方法传入的包名。找到,在头中找到android:name=,即为我们要给ComponentName的构造方法传入的活动名(注:这里的活动名要完整的路径)。

这里以两个APP之间互相调用为例:

其中ThisIsTheFirstOne的androidmanifest.xml文件如下:

XML Code

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="//schemas.android.com/apk/res/android" 

package="com.farsight.thisisthefirstone"

android:versionCode="1"

android:versionName="1.0">

android:minSdkVersion="18"

android:targetSdkVersion="23" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

android:name=".MainActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

在ThisIsTheFirstOne中的活动中我们只使用了一个按钮,用来从这个APP跳到另一个APP。代码如下:

Java Code

package com.farsight.thisisthefirstone;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity{

private Button btn;

private ComponentName componentName;

private String TAG = "This is the First";

@Override

protected void onCreate(Bundle savedInstanceState){

Log.d(TAG, "onCreate");

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v){

// TODO Auto-generated method stub

// "包名","activity名"activity名需包含完整的路径

componentName = new ComponentName("com.farsight.thisisthesecondone",

"com.farsight.thisisthesecondone.MainActivity");

Intent intent = new Intent();

intent.setComponent(componentName);

//告诉设备动作类型

intent.setAction("android.intent.action.MAIN");

//下边这句话是获取APP的特征值的

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//开始跳转

startActivity(intent);

}

});

}

@Override

protected void onDestroy(){

// TODO Auto-generated method stub

super.onDestroy();

Log.d(TAG, "onDestroy");

}

@Override

protected void onStart(){

// TODO Auto-generated method stub

super.onStart();

Log.d(TAG, "onStart");

}

@Override

protected void onRestart(){

// TODO Auto-generated method stub

super.onRestart();

Log.d(TAG, "onRestart");

}

@Override

protected void onResume(){

// TODO Auto-generated method stub

super.onResume();

Log.d(TAG, "onResume");

}

@Override

protected void onStop(){

// TODO Auto-generated method stub

super.onStop();

Log.d(TAG, "onStop");

}

@Override

protected void onPause(){

// TODO Auto-generated method stub

super.onPause();

Log.d(TAG, "onPause");

}

}

第二个APP中,我们的代码大同小异,AndroidManifest.xml文件如下:

Java Code

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="//schemas.android.com/apk/res/android"

package="com.farsight.thisisthesecondone"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="18"

android:targetSdkVersion="23" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

将两个APK都install到设备中,进行调试:

First打开:

点击“我是第一个APP”,跳转到第二个APP:

可以看到,这里第一个APP调用了onPause方法,然后调用了onStop方法,那么接下来我们直接在第二个APP中调用第一个APP:

可以看到,这里第一个APP又一次调用了onCreate方法,也就是说我们通过APK调用的时候是新建了一个APP,也就是说,在虚拟机中重新开了一个进程,这个进程和以前开过的同名APP没有关系。为了验证这一点,我们按硬件返回键:

现在有一个onDestroy被调用了。这个时候晚开的这个TestOne唤醒了睡眠状态的另一个早开的TestOne而不是唤醒跳转到它的TestTwo,这里是我们需要注意的一点。

其后我们再通过硬件返回键:

没有TestOne了,TestTwo被唤醒,再按硬返回键退出结束TestTwo。

在只有一个APK的源码的情况下

上边讨论的情况是我们知道两个需要相互调用的APK的源码的情况,可以看到,其实我们只是用到了androidManifest.xml中的包名和主活动名。

所以当我们不知道一个APK的源码,而又想通过程序调用这个APK的时候,我们只需要想办法得到它的包名和主活动名即可。

方法就是通过一些APK反编译软件来实现,推荐APK Multi-Tool工具,使用方法这里不做过多的介绍,反编译之后可以在相应的文件夹下找到相关的androidManifest.xml文件,和知道两个APK的源码的原理相同,代码类似,这里不再赘述。

上一篇:对linux中的inotify机制的一点认识

下一篇:integer与int的种种比较

热点文章推荐
华清学员就业榜单
高薪学员经验分享
热点新闻推荐
前台专线:010-82525158 企业培训洽谈专线:010-82525379 院校合作洽谈专线:010-82525379 Copyright © 2004-2022 北京华清远见科技集团有限公司 版权所有 ,京ICP备16055225号-5京公海网安备11010802025203号

回到顶部