zoukankan      html  css  js  c++  java
  • Android 开发之:Intent.createChooser() 妙用

    大家对该功能第一印象就是ApiDemo 里面的 其只有区区几行代码  提取为:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("audio/*");
    startActivity(Intent.createChooser(intent, "Select music"));

    执行之 会弹出一个对话框 效果为:

    怎么实现这个呢?

    1. 定义TestActivity 用于根据传入Uri  播放目标

    public class TestActivity extends Activity {
         
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            this.setTitle("TestActivity");
             
            Intent i = this.getIntent();
             
            Uri u = i.getData();
             
            try {
                playMusic(u);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
        public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource(this, uri);
            mp.prepare();
            mp.start();
        }
    }

    2. 在AndroidManifest 注册TestActivity

    <activity android:name=".TestActivity" android:label="TestActivity">
        <intent-filter>
         <action android:name="android.intent.action.GET_CONTENT" />
         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.OPENABLE" />
         <data android:mimeType="audio/music1" />
        </intent-filter>
    </activity>

    3. 使用TestActivity,在外部或者自己程序里调用:

    public void sendChooser(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");
        startActivity(Intent.createChooser(intent, "Select music1 app"));
    }

    最后会弹出选择 TestActivity:

    又比如,打开地图并直接定为到某个经纬度的地方:

    final String uri = String.format("geo:%s,%s?q=%s",
                    checkIn.getLocation().getLatitude(),
                    checkIn.getLocation().getLongitude(),
                    checkIn.getName());
    
    // Show a chooser that allows the user to decide how to display this data, in this case, map data.
    startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse(uri)), getString(R.string.choose)));
  • 相关阅读:
    Eclipse的下载与安装
    IntelliJ IDEA(2018)安装详解
    关于Idea中的web.xml 自动生成模板问题
    基于ssm框架的web.xml配置及解析
    Mybatis分页插件PageHelper的配置与基本使用
    基于maven的ssm框架pom.xml的基本配置及解析
    python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
    快速指数算法
    伪随机序列
    django中Queryset的删除问题、分页问题
  • 原文地址:https://www.cnblogs.com/welhzh/p/6054994.html
Copyright © 2011-2022 走看看