2016-06-23 52 views
0

我有一些應用程序的包名稱,所以現在我需要生成所有應用程序的apks,並希望使用共享意圖發送。如何發送apk使用編程方式在Android的共享意圖

我試過很多可能的解決方案,但沒有找到任何辦法

我用這個鏈接,但事先沒有工作packing my app and share to other + android

感謝

+1

您嘗試過哪些可行的解決方案,爲什麼他們不適合您? –

+0

是的,我使用這個http://stackoverflow.com/questions/21816382/packing-my-app-and-share-to-other-android –

回答

5

我想這應該幫助你

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()))); 

} 
+0

你可以通過多個包名分享多個apks享受:) –

相關問題