【发布时间】:2014-07-20 17:01:46
【问题描述】:
我想在 Intent 中添加 .apk 文件。我想创建一个“共享”按钮,它将通过蓝牙或任何其他能够发送应用程序的应用程序共享整个应用程序。如果这可以通过其他方式完成,请告诉我! 谢谢
【问题讨论】:
标签: android android-intent android-sharing
我想在 Intent 中添加 .apk 文件。我想创建一个“共享”按钮,它将通过蓝牙或任何其他能够发送应用程序的应用程序共享整个应用程序。如果这可以通过其他方式完成,请告诉我! 谢谢
【问题讨论】:
标签: android android-intent android-sharing
List(ApplicationInfo) mAppList=getPackageManager().getInstalledApplications(0);
ApplicationInfo item = mAppList.get(position);
public static void ShareAPK(ApplicationInfo item,Context ctx) {
try {
File srcFile = new File(item.publicSourceDir);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
ctx.startActivity(Intent.createChooser(share, "Sharing"));
} catch (Exception e) {
e.printtrace();
}
}
【讨论】:
使用 ACTION_SEND 操作与设置适当的 MIME 类型并将数据的 URI 放置在一个名为 EXTRA_STREAM 的额外数据中来共享二进制数据。这通常用于共享图像,但也可用于共享任何类型的二进制内容
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("application/vnd.android.package-archive");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
详情;看到这个:Send binary data
【讨论】:
您可以添加应用的 APK 的位置
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Uri uri = Uri.parse("/data/apps/"+getApplicationContext().getPackageName()+".apk");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via"));
请注意,如果您的应用或共享应用无法访问该文件,您可能会收到 SecurityException
【讨论】:
public void shareAPKMine()
{
try {
PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.sourceDir;
File sourceLocation = new File(s);
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/shefi/"+"test.apk";
File targetLocation = new File(path);
Utils.copyDirectory(sourceLocation, targetLocation);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("application/vnd.android.package-archive");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share"+" "+getResources().getString(R.string.app_name)));
}catch (Exception e) {
e.printStackTrace();
}
}
// If targetLocation does not exist, it will be created.
public static void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
// make sure the directory we plan to store the recording in exists
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create dir " + directory.getAbsolutePath());
}
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
【讨论】:
如果您想使用蓝牙在应用内发送(共享).apk 文件,请使用此功能
// Get current ApplicationInfo to find .apk path
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");
// Only use Bluetooth to send .apk
intent.setPackage("com.android.bluetooth");
// Append file and send Intent
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(intent, "Share app"));
【讨论】: