Android 開發之:Intent.createChooser() 妙用

來源:互聯網
上載者:User

Intent.createChooser(ntent target, CharSequence title)

其實 大家對該功能第一影響就是ApiDemo 裡面的 其只有區區幾行代碼  提取為:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);   

  1. intent.setType("audio/*");   
  2. startActivity(Intent.createChooser(intent, "Select music"));  
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 {   
  1.        
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {   
  4.         super.onCreate(savedInstanceState);   
  5.         setContentView(R.layout.main);   
  6.         this.setTitle("TestActivity");   
  7.            
  8.         Intent i = this.getIntent();   
  9.            
  10.         Uri u = i.getData();   
  11.            
  12.         try {   
  13.             playMusic(u);   
  14.         } catch (IllegalArgumentException e) {   
  15.             // TODO Auto-generated catch block   
  16.             e.printStackTrace();   
  17.         } catch (SecurityException e) {   
  18.             // TODO Auto-generated catch block   
  19.             e.printStackTrace();   
  20.         } catch (IllegalStateException e) {   
  21.             // TODO Auto-generated catch block   
  22.             e.printStackTrace();   
  23.         } catch (IOException e) {   
  24.             // TODO Auto-generated catch block   
  25.             e.printStackTrace();   
  26.         }   
  27.     }   
  28.        
  29.     public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{   
  30.         MediaPlayer mp = new MediaPlayer();   
  31.         mp.setDataSource(this, uri);   
  32.         mp.prepare();   
  33.         mp.start();   
  34.     }   
  35. }  
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 blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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<

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.