如何在android中使用共享意图编程发送apk
我有一些应用程序的捆绑名称,所以现在我需要生成所有应用程序的APK,并希望使用共享意图发送 我尝试了很多可能的解决方案,但没有找到任何方法 我使用此链接,但不起作用如何在android中使用共享意图编程发送apk,android,android-intent,share,Android,Android Intent,Share,我有一些应用程序的捆绑名称,所以现在我需要生成所有应用程序的APK,并希望使用共享意图发送 我尝试了很多可能的解决方案,但没有找到任何方法 我使用此链接,但不起作用 提前谢谢我想这会对你有所帮助 ArrayList<Uri> arrayListapkFilepath; // define global //put this code when you wants to share apk arrayListapkFil
提前谢谢我想这会对你有所帮助
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())));
}
arraylistarraylistapkfilepath;//定义全局
//当您想要共享apk时,请输入此代码
arrayListapkFilepath=新的ArrayList();
shareAPK(getPackageName());
//您可以在设备中传递已安装应用程序的捆绑包id,而不是getPackageName()
意向意向=新意向(意向.行动\发送\多次);
setType(“application/vnd.android.package归档”);
intent.putParcelableArrayListExtra(intent.EXTRA\u流,
arrayListapkFilepath);
startActivity(Intent.createChooser)(Intent,“共享”+
arrayListapkFilepath.size()+“文件通过”);
//方法
public void shareAPK(字符串束\u id){
文件f1;
文件f2=空;
最终意图maintent=新意图(Intent.ACTION_MAIN,空);
mainIntent.addCategory(Intent.CATEGORY_启动器);
最终列表pkgAppsList=getPackageManager().QueryInputActivities(mainIntent,0);
int z=0;
for(对象:pkgAppsList){
ResolveInfo=(ResolveInfo)对象;
if(info.activityInfo.packageName.equals(bundle_id)){
f1=新文件(info.activityInfo.applicationInfo.publicSourceDir);
Log.v(“文件--”,
“+f1.getName().toString()+”——“+info.loadLabel(getPackageManager())”;
试一试{
字符串文件_name=info.loadLabel(getPackageManager()).toString();
Log.d(“文件名--”,“”+文件名);
f2=新文件(Environment.getExternalStorageDirectory().toString()+“/Folder”);
f2.mkdirs();
f2=新文件(f2.getPath()+“/”+文件名+“.apk”);
f2.createNewFile();
InputStream in=新文件InputStream(f1);
OutputStream out=新文件OutputStream(f2);
//字节[]buf=新字节[1024];
字节[]buf=新字节[4096];
内伦;
而((len=in.read(buf))>0){
out.write(buf,0,len);
}
in.close();
out.close();
System.out.println(“文件已复制”);
}捕获(FileNotFoundException ex){
System.out.println(例如指定目录中的getMessage()+);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
}
}
添加(Uri.fromFile(新文件(f2.getAbsolutePath()));
}
我认为这应该对您有所帮助
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())));
}
arraylistarraylistapkfilepath;//定义全局
//当您想要共享apk时,请输入此代码
arrayListapkFilepath=新的ArrayList();
shareAPK(getPackageName());
//您可以在设备中传递已安装应用程序的捆绑包id,而不是getPackageName()
意向意向=新意向(意向.行动\发送\多次);
setType(“application/vnd.android.package归档”);
intent.putParcelableArrayListExtra(intent.EXTRA\u流,
arrayListapkFilepath);
startActivity(Intent.createChooser)(Intent,“共享”+
arrayListapkFilepath.size()+“文件通过”);
//方法
public void shareAPK(字符串束\u id){
文件f1;
文件f2=空;
最终意图maintent=新意图(Intent.ACTION_MAIN,空);
mainIntent.addCategory(Intent.CATEGORY_启动器);
最终列表pkgAppsList=getPackageManager().QueryInputActivities(mainIntent,0);
int z=0;
for(对象:pkgAppsList){
ResolveInfo=(ResolveInfo)对象;
if(info.activityInfo.packageName.equals(bundle_id)){
f1=新文件(info.activityInfo.applicationInfo.publicSourceDir);
Log.v(“文件--”,
“+f1.getName().toString()+”——“+info.loadLabel(getPackageManager())”;
试一试{
字符串文件_name=info.loadLabel(getPackageManager()).toString();
Log.d(“文件名--”,“”+文件名);
f2=新文件(Environment.getExternalStorageDirectory().toString()+“/Folder”);
f2.mkdirs();
f2=新文件(f2.getPath()+“/”+文件名+“.apk”);
f2.createNewFile();
InputStream in=新文件InputStream(f1);
OutputStream out=新文件OutputStream(f2);
//字节[]buf=新字节[1024];
字节[]buf=新字节[4096];
内伦;
而((len=in.read(buf))>0){
out.write(buf,0,len);
}
in.close();
out.close();
System.out.println(“文件已复制”);
}捕获(FileNotFoundException ex){
System.out.println(例如指定目录中的getMessage()+);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
}
}
添加(Uri.fromFile(新文件(f2.getAbsolutePath()));
}
您尝试过哪些可能的解决方案,为什么它们不适用于您?是的,我使用此选项您尝试过哪些可能的解决方案,为什么它们不适用于您?是的,我使用此选项您可以传递多个包名以共享多个APK享受:)您可以传递多个包名以共享多个APK享受:)