Android会根据Intent的属性去系统里查找合适的Activity来启动。有时候Intent的属性不是很充分,可能有多个Activity可以被启动,此时,我们可以调用系统的Intents chooser来将可能的所有Activity提供给用户选择。

官方示例:

public class Intents extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.intents);        // Watch for button clicks.        Button button = (Button)findViewById(R.id.get_music);        button.setOnClickListener(mGetMusicListener);    }    private OnClickListener mGetMusicListener = new OnClickListener() {        public void onClick(View v) {            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);            intent.setType("audio/*");            startActivity(Intent.createChooser(intent, "Select music"));        }    };}

设置Intent的action为Intent.ACTION_GET_CONTENT,type为audio/*,然后调用Intent.createChooser(intent, "Select music")启动Intent选择器。

运行效果:



如果将startActivity(Intent.createChooser(intent, "Select music"))改为startActivity(intent),即不在代码里显示调用Intent选择器。但是系统会调用默认的Intent选择器。

运行效果如下:


更多相关文章

  1. 使用响应式编程(RxJava)开发Android(安卓)App
  2. 如何将编译好的apk文件加入Android系统?
  3. Android(安卓)4.2 Ethernet启动流程
  4. Android(安卓)ApiDemo学习(二)notification——1 IncomingMessage
  5. [Android]ActivityUnitTestCase解释
  6. android开机自启动失败的原因
  7. Android(安卓)EditText设置InputType属性时,隐藏系统软键盘失败
  8. android wifiservice enable流程
  9. Android(安卓)Intent.FLAG详解

随机推荐

  1. 【转】 Android(安卓)Layout之三:Ralative
  2. Lottie Android(安卓)初探
  3. Android(安卓)FrameWork——Binder机制详
  4. Android培训班(103)内核入口汇编3
  5. Android(安卓)SharedPreferences存储的正
  6. Android(安卓)APP tcpdump抓包方法
  7. android使用datepicker计算年龄
  8. Android(安卓)自定义控件实现ListView索
  9. Android(安卓)操作蓝牙Ble时数据转换的常
  10. Android(安卓)4.0 编译全程