我为我的应用创建了一个共享按钮,它将通过Intent共享我的整个应用程序..它的工作原理如下:
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());
}
现在这将使用我的软件包名称发送我的应用程序..我需要在共享之前用我的应用程序名称重命名它。比如这样:
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(
packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA));
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
String shareName = appName+"_"+"v"+version;
有没有办法用这个“shareName”字符串重命名我的包名?
答案 0 :(得分:2)
正如@CommonsWare所提到的,您可以通过在路径中创建应用程序备份,然后从该路径共享它来实现:
public void shareApp(String package_name) {
try {
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(
packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA));
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
String shareName = appName+"_"+"v"+version;
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);
for (Object object : pkgAppsList) {
ResolveInfo info = (ResolveInfo) object;
if (info.activityInfo.packageName.equals(package_name)) {
f1 = new File(info.activityInfo.applicationInfo.publicSourceDir);
try {
f2 = new File(Environment.getExternalStorageDirectory().toString() + "/Apps");
f2.mkdirs();
f2 = new File(f2.getPath() + "/" + shareName + ".apk");
f2.createNewFile();
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
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());}
}
}
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f2));
startActivity(Intent.createChooser(share, "PersianCoders"));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
}
在任何你喜欢的地方打电话:
shareApp(getPackageName());
答案 1 :(得分:0)
有没有办法用这个“shareName”字符串重命名我的包名?
不直接。 ACTION_SEND
中没有“使用此备用文件名”选项。
你可以:
在新文件名下制作原始文件的副本,然后共享该副本,或
实施ContentProvider
,在收到带有新文件名的Uri
时返回文件的内容
后者更复杂,但也解决了您的Uri.fromFile()
问题:在Android 7.0及更高版本的设备上运行时,将targetSdkVersion
提升至24或更高时无效。