Android之共享已安装的apk应用
是否遇到过自己手机上有好的应用,但不知道怎么分享给好友的时候呢?作为一名程序员,遇到了这样简单的问题,肯定要想办法解决,经过google和百度,查得android上所有已安装的应用都会做一个备份,分别存放在三个地方:
1、系统签名的软件:/system/app
2、安装到内存上的非系统签名软件:/data/app
3、安装到sd卡上的非系统签名软件:/mnt/asec/包名-数字/pkg.apk
我们可以使用re浏览器查看和分享这些apk,本人已确认过,这些应用都是可用的,但是这样子相当麻烦,因为你自己必须记得这三个位置,以及这三个位置对应的存放的应用,否则每次都要把三个文件夹浏览一次,多麻烦呀,程序就是为了让人们偷懒而生的,因此废话不多说,下面贴出我自己写的可列出所有安装应用并能够通过蓝牙分享的代码:
[java]
public class MainActivity extends Activity {
GridView mGridView = null; www.zzzyk.com
List<ResolveInfo> mAllApps = new ArrayList<ResolveInfo>();
List<PackageInfo>mAllPackages=new ArrayList<PackageInfo>();
PackageManager packageManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showView();
}
public void showView() {
packageManager = getPackageManager();
mGridView = (GridView) this.findViewById(R.id.gridView1);
setMyAllApps();
mGridView.setAdapter(new MyAdapter(mAllApps, this));
mGridView.setNumColumns(4);
mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ResolveInfo resolveInfo=mAllApps.get(arg2);
String packageName=resolveInfo.activityInfo.packageName;
for(Iterator<PackageInfo>iterator=mAllPackages.iterator();iterator.hasNext();){
PackageInfo packageInfo=iterator.next();
if(packageInfo.applicationInfo.packageName.equals(packageName)){
Log.i("six grade", "source dir:"+packageInfo.applicationInfo.sourceDir);
File sourceFile=new File(packageInfo.applicationInfo.sourceDir);
//调用android系统的分享窗口
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sourceFile));
startActivity(intent);
}
}
return true;
}
});
}
public void setMyAllApps() {
// 查找所有首先显示的activity
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = packageManager.queryIntentActivities(intent, 0);
mAllPackages=packageManager.getInstalledPackages(0);
// 按照名字排序
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(
packageManager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class MyAdapter extends BaseAdapter {
List<ResolveInfo> appList;
Context mContext;
public MyAdapter(List<ResolveInfo> appList, Context context) {
this.appList = appList;
mContext = context;
}
public int getCount() {
// TODO Auto-generated method stub
return appList.size();
}
&nb
补充:移动开发 , Android ,