Android之共用已安裝的apk應用

來源:互聯網
上載者:User

         是否遇到過自己手機上有好的應用,但不知道怎麼分享給好友的時候呢?作為一名程式員,遇到了這樣簡單的問題,肯定要想辦法解決,經過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.2cto.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(); 
        } 
 
        public Object getItem(int position) { 
            // TODO Auto-generated method stub 
            return null; 
        } 
 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub 
            return 0; 
        } 
 
        public View getView(int position, View convertView, ViewGroup parent) { 
            // TODO Auto-generated method stub 
            convertView = LayoutInflater.from(mContext).inflate( 
                    R.layout.application, null); 
            ImageView imageView = (ImageView) convertView 
                    .findViewById(R.id.imageView1); 
            TextView textView = (TextView) convertView 
                    .findViewById(R.id.textView1); 
            ResolveInfo resolveInfo = appList.get(position); 
            textView.setText(resolveInfo.loadLabel(packageManager)); 
            imageView.setBackgroundColor(Color.TRANSPARENT); 
            Bitmap iconBitmap = ImageUtils.drawableToBitmap(resolveInfo 
                    .loadIcon(packageManager)); 
            imageView.setImageBitmap(ImageUtils.getRoundedCornerBitmap( 
                    ImageUtils.zoomBitmap(iconBitmap, 60, 60), 10)); 
            return convertView; 
        } 
 
    } 

下面是程式


我使用的是gridview把所有已安裝的使用者應用列出來,但是沒有做搜尋,以後有時間肯定會加上,相信肯定會為你帶來方便的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.