码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- queryIntentActivities

时间:2015-02-11 23:16:12      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

某些时候你想要知道某个APP是否有注册了一个明确的intent,比如说你想要检查某个receiver是否存在,然后根据是否存在来这个receiver来在你的AP里面enable某些功能。我们可以通过PackageManager来check它。

code

public boolean isIntentAvailable(Context context, String action) {  
    final PackageManager packageManager = context.getPackageManager();  
    final Intent intent = new Intent(action);  
    List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  
   if (resolveInfo.size() > 0) {  
        return true;  
    }  
   return false;  
}

干货

一般APP的manifest里面要设置为launcher的activity总是要设置intent-filter:

<intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>

可以通过queryIntentActivities来获得有哪些launcher:

private void loadApps() {           
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);           
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);             
        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);       
    }

我们可以在自己的app上用一个gridview显示出当前手机有哪些app:

import java.util.List;       
import android.app.Activity;   
import android.content.Intent;   
import android.content.pm.ResolveInfo;   
import android.os.Bundle;   
import android.view.View;   
import android.view.ViewGroup;   
import android.widget.BaseAdapter;   
import android.widget.GridView;   
import android.widget.ImageView;       
public class MyHome extends Activity   
{         
    GridView mGrid;             
     
    @Override    
   public void onCreate(Bundle savedInstanceState) {           
        super.onCreate(savedInstanceState);                     
        loadApps();           
        setContentView(R.layout.main);           
        mGrid = (GridView) findViewById(R.id.apps_list);           
        mGrid.setAdapter(new AppsAdapter());       
    }        
           
    private List<ResolveInfo> mApps;  
         
    private void loadApps() {           
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);           
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);              
        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);      
    }    
       
    public class AppsAdapter extends BaseAdapter   
    {           
        public AppsAdapter() {         }               
        public View getView(int position, View convertView, ViewGroup parent) {               
            ImageView i;                   
            if (convertView == null) {                   
                i = new ImageView(MyHome.this);                   
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);                   
                i.setLayoutParams(new GridView.LayoutParams(50, 50));               
            } else {                   
                i = (ImageView) convertView;               
            }                   
            ResolveInfo info = mApps.get(position);              
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));                   
            return i;           
        }                   
        public final int getCount() {              
            return mApps.size();           
        }               
        public final Object getItem(int position) {               
            return mApps.get(position);           
        }               
        public final long getItemId(int position) {               
            return position;           
        }       
    }   
}

可以设置点击监听器去开启app:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {            
    ResolveInfo info = mApps.get(position);                          
    //该应用的包名            
    String pkg = info.activityInfo.packageName;               
    //应用的主activity类            
    String cls = info.activityInfo.name;                           
    ComponentName componet = new ComponentName(pkg, cls);                          
    Intent i = new Intent();              
    i.setComponent(componet);              
    startActivity(i);          
}

我是天王盖地虎的分割线

Android -- queryIntentActivities

标签:

原文地址:http://www.cnblogs.com/yydcdut/p/4287077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!