从一个应用程序中启动令一个应用程序
启动程序的代码:
Intent intent = new Intent();
intent.setAction("com.netvista.bj.entry");
startActivity(intent);
被启动程序的Androidmanifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netvista.bj"
android:versionCode="33"
android:sharedUserId="com.vixtel.netvista"
android:versionName="2.0.1.33" >
<application android:name=".app.MobileIqApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".app.NetDiagMainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.netvista.bj.entry" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".app.WifiDiagnosisActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
</activity>
<activity android:name=".app.SettingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".testagent.TestAgentService"/>
</application>
</manifest>
2 安装apk方法(apk文件已存在SD根目录下)
启动程序的代码:
Intent intent = new Intent();
intent.setAction("com.netvista.bj.entry");
startActivity(intent);
被启动程序的Androidmanifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netvista.bj"
android:versionCode="33"
android:sharedUserId="com.vixtel.netvista"
android:versionName="2.0.1.33" >
<application android:name=".app.MobileIqApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".app.NetDiagMainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.netvista.bj.entry" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".app.WifiDiagnosisActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
</activity>
<activity android:name=".app.SettingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".testagent.TestAgentService"/>
</application>
</manifest>
2 安装apk方法(apk文件已存在SD根目录下)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+
"/SpeedClient-Bj.apk"),"application/vnd.android.package-archive");
mContext.startActivity(intent);//mContext= this;
3将assets文件提取出来并放到SD根目录下:
public boolean copyApkFromAssets(Context context, String fileName, String path) {
boolean copyIsFinish = false;
try {
InputStream is = context.getAssets().open(fileName);
Toast.makeText(this, path,0).show();
File file = new File(path);
file.createNewFile();
Toast.makeText(this, fileName,0).show();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
} catch (IOException e) {
e.printStackTrace();
}
return copyIsFinish;
}
调用copyApkFromAssets(this, "SpeedBj.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/SpeedBj.apk"));// 将SpeedBj.apk复制到SD根目录,当然得有写SD卡的权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+
"/SpeedClient-Bj.apk"),"application/vnd.android.package-archive");
mContext.startActivity(intent);//mContext= this;
3将assets文件提取出来并放到SD根目录下:
public boolean copyApkFromAssets(Context context, String fileName, String path) {
boolean copyIsFinish = false;
try {
InputStream is = context.getAssets().open(fileName);
Toast.makeText(this, path,0).show();
File file = new File(path);
file.createNewFile();
Toast.makeText(this, fileName,0).show();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
} catch (IOException e) {
e.printStackTrace();
}
return copyIsFinish;
}
调用copyApkFromAssets(this, "SpeedBj.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/SpeedBj.apk"));// 将SpeedBj.apk复制到SD根目录,当然得有写SD卡的权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
本文介绍如何通过Intent在Android应用程序中启动另一个应用程序的方法,并提供具体的代码示例,包括启动指定Activity和安装APK文件的实现。

1万+

被折叠的 条评论
为什么被折叠?



