Sharing specific apk from listview

2019-08-07 16:18发布

问题:

Hey guys i have got listview which contains installed apk's i want to share the specific apk on which the users click.I have got some code when i click on share "share via bluetooth "pops up but it didnt get received on my other device.

public class MainActivity extends ListActivity {
PackageManager packageManager;
List<ApplicationInfo> applist;
Listadapter listadapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    packageManager = getPackageManager();

    new LoadApplications().execute();

}

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Choose option")
            .setItems(R.array.options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {

                        case 0:

                            ApplicationInfo app2 = applist.get(position);
                            Intent sharei=new Intent(Intent.ACTION_SEND);
                            Uri uri=Uri.parse("package:"+app2.packageName);
                            sharei.putExtra(Intent.EXTRA_STREAM,uri);
                            sharei.setType("application/vnd.android.package-archive");
                            startActivity(Intent.createChooser(sharei, "share"));
                            break;

                    }
                }
            });
    dialogBuilder.setCancelable(true).show();

 }

 private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<>();

    for (ApplicationInfo info : list) {
        try {
            if  (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
                applist.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return applist;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

        listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(aVoid);

    }

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,");
        super.onPreExecute();
    }

}

}

Wat am i dng wrong here pls help with some code if possible.Thanx!!!

回答1:

You cant try this in inListItemClick in case 0 instead of your code try this.

ApplicationInfo app2 = applist.get(position);
File srcFile = new File(app2.publicSourceDir);
Intent sharei=new Intent(Intent.ACTION_SEND);
sharei.setType("application/vnd.android.package-archive");
sharei.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(srcFile));   
startActivity(Intent.createChooser(sharei, "share"));