API Demos 2.2 研读笔记(12)——Intents Chooser
16lz
2021-01-26
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选择器。
运行效果如下:
更多相关文章
- 使用响应式编程(RxJava)开发Android(安卓)App
- 如何将编译好的apk文件加入Android系统?
- Android(安卓)4.2 Ethernet启动流程
- Android(安卓)ApiDemo学习(二)notification——1 IncomingMessage
- [Android]ActivityUnitTestCase解释
- android开机自启动失败的原因
- Android(安卓)EditText设置InputType属性时,隐藏系统软键盘失败
- android wifiservice enable流程
- Android(安卓)Intent.FLAG详解