當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > APK之間的互相調(diào)用
APK之間的互相調(diào)用,主要的實現(xiàn)方法就是通過intent.setComponent(componentName),
Java Code
// "包名","activity名"activity名需包含完整的路徑
componentName = new ComponentName("com.farsight.thisisthesecondone",
"com.farsight.thisisthesecondone.MainActivity");
Intent intent = new Intent();
intent.setComponent(componentName);
//告訴設(shè)備動作類型
intent.setAction("android.intent.action.MAIN");
//下邊這句話是獲取APP的特征值的
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//開始跳轉(zhuǎn)
startActivity(intent);
在擁有兩個APK的源碼的情況下:
直接閱讀兩個源碼的androidmanifest.xml文件,找到其中的頭中的package=項,即為我們要調(diào)用的時候給ComponentName的構(gòu)造方法傳入的包名。找到,在頭中找到android:name=,即為我們要給ComponentName的構(gòu)造方法傳入的活動名(注:這里的活動名要完整的路徑)。
這里以兩個APP之間互相調(diào)用為例:
其中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);
//告訴設(shè)備動作類型
intent.setAction("android.intent.action.MAIN");
//下邊這句話是獲取APP的特征值的
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//開始跳轉(zhuǎn)
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到設(shè)備中,進(jìn)行調(diào)試:
First打開:
點擊“我是第一個APP”,跳轉(zhuǎn)到第二個APP:
可以看到,這里第一個APP調(diào)用了onPause方法,然后調(diào)用了onStop方法,那么接下來我們直接在第二個APP中調(diào)用第一個APP:
可以看到,這里第一個APP又一次調(diào)用了onCreate方法,也就是說我們通過APK調(diào)用的時候是新建了一個APP,也就是說,在虛擬機(jī)中重新開了一個進(jìn)程,這個進(jìn)程和以前開過的同名APP沒有關(guān)系。為了驗證這一點,我們按硬件返回鍵:
現(xiàn)在有一個onDestroy被調(diào)用了。這個時候晚開的這個TestOne喚醒了睡眠狀態(tài)的另一個早開的TestOne而不是喚醒跳轉(zhuǎn)到它的TestTwo,這里是我們需要注意的一點。
其后我們再通過硬件返回鍵:
沒有TestOne了,TestTwo被喚醒,再按硬返回鍵退出結(jié)束TestTwo。
在只有一個APK的源碼的情況下
上邊討論的情況是我們知道兩個需要相互調(diào)用的APK的源碼的情況,可以看到,其實我們只是用到了androidManifest.xml中的包名和主活動名。
所以當(dāng)我們不知道一個APK的源碼,而又想通過程序調(diào)用這個APK的時候,我們只需要想辦法得到它的包名和主活動名即可。
方法就是通過一些APK反編譯軟件來實現(xiàn),推薦APK Multi-Tool工具,使用方法這里不做過多的介紹,反編譯之后可以在相應(yīng)的文件夾下找到相關(guān)的androidManifest.xml文件,和知道兩個APK的源碼的原理相同,代碼類似,這里不再贅述。