我想通过用户设备上所有可能安装的应用分享我的应用.. 我的意思是我的apk文件本身不仅仅是我的应用程序的链接.. 我怎么能这样做?
答案 0 :(得分:1)
首先,SO不是租用服务的开发人员。你应该试试 什么,然后如果你遇到障碍,然后在这里问 帮助,描述你的问题。
修改2
找到另一段代码here
try {
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0);
File srcFile = new File(ai.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));
startActivity(Intent.createChooser(share, "PersianCoders"));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
修改强>
尝试此代码(来自Here)
ArrayList<Uri> arrayListapkFilepath; // define global
//put this code when you wants to share apk
arrayListapkFilepath = new ArrayList<Uri>();
shareAPK(getPackageName());
// you can pass bundle id of installed app in your device instead of getPackageName()
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("application/vnd.android.package-archive");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
arrayListapkFilepath);
startActivity(Intent.createChooser(intent, "Share " +
arrayListapkFilepath.size() + " Files Via"));
//Method
public void shareAPK(String bundle_id) {
File f1;
File f2 = null;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
int z = 0;
for (Object object : pkgAppsList) {
ResolveInfo info = (ResolveInfo) object;
if (info.activityInfo.packageName.equals(bundle_id)) {
f1 = new File(info.activityInfo.applicationInfo.publicSourceDir);
Log.v("file--",
" " + f1.getName().toString() + "----" + info.loadLabel(getPackageManager()));
try {
String file_name = info.loadLabel(getPackageManager()).toString();
Log.d("file_name--", " " + file_name);
f2 = new File(Environment.getExternalStorageDirectory().toString() + "/Folder");
f2.mkdirs();
f2 = new File(f2.getPath() + "/" + file_name + ".apk");
f2.createNewFile();
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
// byte[] buf = new byte[1024];
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage() + " in the specified directory.");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
arrayListapkFilepath.add(Uri.fromFile(new File(f2.getAbsolutePath())));
}
旧答案 要回答您的问题,出于显而易见的原因,无法通过“应用程序通过每个可能安装的应用程序”共享您的“apk”。当然你可以编写代码来通过蓝牙或局域网Wifi分享你的apk,你可以将你的apk附加到邮件/支持的消息服务。但是不可能像“你把那个”那样做。
您可以使用此代码将apk复制到某个地方
File file = new File(getPackageManager().getPackageInfo(getPackageName(), 0).publicSourceDir);
// Copy the .apk file to wherever
您可以通过附加您保存/复制的apk,使用蓝牙/ Wifi / Intent与ACTION_SEND分享。但据我所知,很少有Messagin应用允许发送apk文件。
此Article将帮助您共享文件: