Android調用系統分享功能以及createChooser的使用


工程結構

//效果圖  

點擊測試分享                                                                                                          點擊createChoose妙用

     

主要是看右邊的,可不是用什么Dialog來搞的哦,而是你Activity程序,可以激活進去了

提示:這個東西可以延伸到一個音頻文件,打開時,可以調用你的音樂播放器來播放哦,視頻,圖片,也是類似,可以調用你自己的東西

當然,前提是你的manifest.xml里的東西要配置對呀

<data Android:mimeType="mark/nimei" />  

如下

<activity android:name=".TestActivity"
            android:label="你妹啊"
            >
            <intent-filter>  
                <action android:name="android.intent.action.XXMM" />  
                 <category android:name="android.intent.category.DEFAULT" />  
                 <category android:name="android.intent.category.OPENABLE" />  
                 <data android:mimeType="mark/nimei" />  
            </intent-filter>  
        </activity>
        <activity android:name=".Test2Activity"
            android:label="你妹啊2"
            >
            <intent-filter>  
                <action android:name="android.intent.action.XXMM" />  
                 <category android:name="android.intent.category.DEFAULT" />  
                 <category android:name="android.intent.category.OPENABLE" />  
                 <data android:mimeType="mark/nimei" />  
            </intent-filter>  
        </activity>

 

//代碼如下:

[java]  view plain copy
  1. package com.mark.share.demo;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class AppShareDemoActivity extends Activity  
  14. {  
  15.     private Button testshare;  
  16.     private Button createChooserBtn;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)  
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         testshare=(Button) findViewById(R.id.testshare);  
  24.         createChooserBtn=(Button) findViewById(R.id.Test_createChooser);  
  25.           
  26.         testshare.setOnClickListener(new OnClickListener()  
  27.         {  
  28.               
  29.             @Override  
  30.             public void onClick(View v)  
  31.             {  
  32.                 Intent intent = new Intent(Intent.ACTION_SEND);  
  33.                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  34.                 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("sdcard/1.png")));  //傳輸圖片或者文件 采用流的方式  
  35.                 intent.putExtra(Intent.EXTRA_TEXT, "分享分享微博");   //附帶的說明信息  
  36.                 intent.putExtra(Intent.EXTRA_SUBJECT, "標題");  
  37.                 intent.setType("image/*");   //分享圖片  
  38.                 startActivity(Intent.createChooser(intent,"分享"));  
  39.             }  
  40.         });  
  41.           
  42.         createChooserBtn.setOnClickListener(new OnClickListener()  
  43.         {  
  44.             @Override  
  45.             public void onClick(View v)  
  46.             {  
  47.                 Intent intent = new Intent();  
  48.                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  49.                 intent.setAction("android.intent.action.XXMM");  
  50.                 intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "mark/nimei");    
  51.                 startActivity(Intent.createChooser(intent, "Select music1 app"));    
  52.             }  
  53.         });  
  54.     }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM