安卓利用Intent分享資訊

來源:互聯網
上載者:User

最基本分享方法:

Intent intent = new Intent(Intent.ACTION_SEND); // 啟動分享發送的屬性
intent.setType("text/plain"); // 分享發送的資料類型
intent.setPackage(packAgeName);
intent.putExtra(Intent.EXTRA_SUBJECT, "xx分享"); // 分享的主題
intent.putExtra(Intent.EXTRA_TEXT, "分享內容"); // 分享的內容
mActivity.startActivity(Intent.createChooser(intent, "選擇分享"));// 目標應用選擇對話方塊的標題 

如果需要對一些應用進行過濾顯示的話,可以採用這種方法:

這個方法是用來擷取安卓系統上所有符合你需要分享的應用:

public List<ResolveInfo> getShareTargets(){
        List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
        Intent intent=new Intent(Intent.ACTION_SEND,null);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setType("text/plain");
        PackageManager pm=mActivity.getPackageManager();
        mApps=pm.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        return mApps;

    } 

 假如你想通過簡訊分享,但是你寫了一個簡訊軟體,想通過你的簡訊軟體來發送資訊,那麼你就可以在擷取到的列表中判斷com.android.mms這個包名的應用,點擊該選項的時候跳至你需要的頁面。

 一個例子如下:

List<ResolveInfo> appList=getShareTargets();
items=new String[appList.size()];
if(appList.size()>0){
  for (int i = 0; i < appList.size(); i++) {
ResolveInfo tmp_ri=(ResolveInfo)appList.get(i);
        ActivityInfo tmp_ai=tmp_ri.activityInfo;
        ApplicationInfo apinfo=tmp_ai.applicationInfo;
        String tmp_appName=apinfo.loadLabel(mActivity.getPackageManager()).toString();
        appInfo.put(tmp_appName,tmp_ai.packageName);
        items[i]=tmp_appName;
  }
}
else{
       Toast.makeText(mActivity.getApplicationContext(),"暫無分享應用", Toast.LENGTH_SHORT).show();
}
new AlertDialog.Builder(mActivity).setTitle("選擇分享") 
    .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
     if(appInfo.size()>0){
         String packAgeName=appInfo.get(items[item].toString()).toString();
         if(packAgeName.equals("com.android.mms")){
         Intent gsms = new Intent(mActivity, sendSMSActivity.class);
         Bundle b = new Bundle();
         b.putString("content", "分享內容");
         gsms.putExtras(b);
         mActivity.startActivity(gsms);
         }
     else{
         Intent intent = new Intent(Intent.ACTION_SEND); // 啟動分享發送的屬性
         intent.setType("text/plain"); // 分享發送的資料類型
         intent.setPackage(packAgeName);
         intent.putExtra(Intent.EXTRA_SUBJECT, "xx分享"); // 分享的主題
         intent.putExtra(Intent.EXTRA_TEXT, "分享內容"); // 分享的內容
         mActivity.startActivity(Intent.createChooser(intent, "選擇分享"));// 目標應用選擇對話方塊的標題
        }
   }
else{
     Toast.makeText(mActivity.getApplicationContext(),"暫無分享應用", Toast.LENGTH_SHORT).show();

 }
     dialog.cancel(); 
 } 

}).show();//顯示對話方塊  

相關文章

聯繫我們

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