2011-09-21 10 views
19

मैं कुछ साझा करने के लिए एंड्रॉइड Intent.ACTION_SEND का उपयोग करना चाहता हूं। तो मैं इस तरह एक साझा सूची मिल गया: Sharing intent listIntent.ACTION_SEND में कौन सा इरादा चुना गया है, यह जानने के लिए कैसे?

लेकिन मैं इस तरह के रूप प्रत्येक कार्य के लिए अलग-अलग सामग्री साझा करना चाहते हैं,:

  • तो बंटवारे ईमेल/Gmail द्वारा, सामग्री "ईमेल द्वारा शेयर किया जाना चाहिए "।

  • यदि फेसबुक द्वारा साझा किया जाता है, तो सामग्री "फेसबुक द्वारा साझा करें" होना चाहिए।

तो, क्या ऐसा करना संभव है?

+0

जहां समस्या है? आप क्लिक किए गए आइटम के आधार पर इरादा भेजते हैं ?! समस्या –

+0

आह कहां है, मेरा मतलब है कि मैं कैसे जान सकता हूं कि साझा करने के लिए कौन सा इरादा चुना गया है? – anticafe

+0

इतना बुरा, http://stackoverflow.com/questions/6137592/how-to-now-the-action-choosed-in-a-intent-createchooser और http://stackoverflow.com/questions/4417019/ के अनुसार कैसे-से-उपयोगकर्ता-चयन-से-startactivityforresultintent-createchooserfi, मुझे नहीं पता कि कौन सा इरादा उपयोगकर्ता चुनता है। – anticafe

उत्तर

26

आपको ऐसी जानकारी नहीं मिल सकती है।

जब तक आप गतिविधि चयन के लिए संवाद का अपना कार्यान्वयन नहीं करते हैं।

इस तरह के संवाद को बनाने के लिए आपको PackageManager और इसके queryIntentActivities() फ़ंक्शन का उपयोग करने की आवश्यकता है। समारोह List<ResolveInfo> देता है।

ResolveInfo एक गतिविधि (resolveInfo.activityInfo.packageName) के बारे में कुछ जानकारी है, और PackageManager की मदद से आप अन्य जानकारी (संवाद में गतिविधि को प्रदर्शित करने के लिए उपयोगी) प्राप्त कर सकते हैं - आवेदन आइकन drawable, आवेदन लेबल, ...।

परिणामों को एक संवाद में सूची में प्रदर्शित करें (या एक संवाद के रूप में स्टाइल की गई गतिविधि में)। जब कोई आइटम क्लिक किया जाता है तो नया इरादा .ACTION_SEND बनाएं, अपनी इच्छित सामग्री जोड़ें और चयनित गतिविधि का पैकेज जोड़ें (intent.setPackage(pkgName))।

5

यह सुनिश्चित नहीं है कि आप अभी भी एक उत्तर की तलाश में हैं, लेकिन क्लिकक्लिकक्लेक में एक उदाहरण कार्यान्वयन है कि आप ACTION_SEND इरादे को कैसे रोक सकते हैं और पैकेज नाम के आधार पर चुन सकते हैं और कुछ विशेषताओं का क्या हो रहा है। टोमिक द्वारा वर्णित अधिकांश चरणों में शामिल है। इस कार्यान्वयन के बारे में

http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

एक शक्तिशाली पहलू आप अपने कॉल करने के लिए एनालिटिक्स जोड़ सकते हैं।

+0

यह है एक शानदार कार्यान्वयन। – TomaszRykala

11

वहाँ जानकारी इस तरह का उपयोग करने की प्रत्यक्ष विधि नहीं है ....

चरण 1: अपने कोड तुम सब एक एडाप्टर जो सूची के अपने कस्टम दृश्य में शामिल होंगे पर साझा करने के लिए की घोषणा करने की जरूरत के पहले अंदर ...

//sharing implementation 
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 

        // what type of data needs to be send by sharing 
        sharingIntent.setType("text/plain"); 

        // package names 
        PackageManager pm = getPackageManager(); 

        // list package 
        List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0); 

        objShareIntentListAdapter = new ShareIntentListAdapter(CouponView.this,activityList.toArray()); 

        // Create alert dialog box 
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); 
        builder.setTitle("Share via"); 
        builder.setAdapter(objShareIntentListAdapter, new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int item) { 

          ResolveInfo info = (ResolveInfo) objShareIntentListAdapter.getItem(item); 

          // if email shared by user 
          if(info.activityInfo.packageName.contains("Email") 
            || info.activityInfo.packageName.contains("Gmail") 
            || info.activityInfo.packageName.contains("Y! Mail")) { 

           PullShare.makeRequestEmail(COUPONID,CouponType); 
          } 

          // start respective activity 
          Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
          intent.setClassName(info.activityInfo.packageName, info.activityInfo.name); 
          intent.setType("text/plain"); 
          intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName); 
          intent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+shortURL); 
          intent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+shortURL);                
          ((Activity)context).startActivity(intent);            

         }// end onClick 
        }); 

        AlertDialog alert = builder.create(); 
        alert.show(); 

चरण 2: अब आप अपने एडाप्टर के लिए एक लेआउट inflater बनाने (ShareIntentListAdapter.java)

package com.android; 

import android.app.Activity; 
import android.content.pm.ResolveInfo; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ShareIntentListAdapter extends ArrayAdapter{ 

    private final Activity context; 
    Object[] items; 


    public ShareIntentListAdapter(Activity context,Object[] items) { 

     super(context, R.layout.social_share, items); 
     this.context = context; 
     this.items = items; 

    }// end HomeListViewPrototype 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 

     LayoutInflater inflater = context.getLayoutInflater(); 

     View rowView = inflater.inflate(R.layout.social_share, null, true); 

     // set share name 
     TextView shareName = (TextView) rowView.findViewById(R.id.shareName); 

     // Set share image 
     ImageView imageShare = (ImageView) rowView.findViewById(R.id.shareImage); 

     // set native name of App to share 
     shareName.setText(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString()); 

     // share native image of the App to share 
     imageShare.setImageDrawable(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager())); 

     return rowView; 
    }// end getView 

}// end main onCreate 

चरण 3: अपने XML लेआउट Ty बनाएं संवाद बॉक्स में सूची दृश्य दिखाने के लिए pe (social_share।एक्सएमएल)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/categoryCell" 
    android:layout_width="match_parent" 
    android:layout_height="30dip" 
    android:background="@android:color/white" > 

    <TextView 
     android:id="@+id/shareName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginBottom="15dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="15dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#000000" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/shareImage" 
     android:layout_width="35dip" 
     android:layout_height="35dip" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:contentDescription="@string/image_view" /> 

</RelativeLayout> 

// vKj 
+0

मैं कई क्लिकों को रोकने के लिए IF से घिरा हुआ चेतावनी। शो() को बदलने की भी सिफारिश करता हूं। अगर (! alert.isShowing()) {alert.show(); } – jmnwong

1

Tomik महान जवाब का उपयोग करते हुए मैं अपने कस्टम शेयर PackageManager loadLabel और LoadIcon का उपयोग कर सूची उत्पादन करने में सक्षम हूँ:

public class MainActivity extends FragmentActivity 
{ 

    ArrayList<Drawable> icons; 
    ArrayList<String> labels; 

    @Override 
    protected void onCreate(Bundle arg0) { 
     // TODO Auto-generated method stub 
     super.onCreate(arg0); 
     setContentView(R.layout.activity_main); 
     icons=new ArrayList<Drawable>(); 
     labels=new ArrayList<String>(); 
     PackageManager manager=getPackageManager(); 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("text/plain"); 
     List<ResolveInfo> activities=manager. 
       queryIntentActivities(intent,0); 
     for(int i=0;i<activities.size();i++) 
     { 
      ApplicationInfo appInfo=null; 
      try { 
       appInfo=manager.getApplicationInfo(activities.get(i).activityInfo.packageName,0); 
       labels.add(name=appInfo.loadLabel(getPackageManager()).toString()); 
      } catch (NameNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      icons.add(appInfo.loadIcon(getPackageManager())); 
     } 
    } 

} 
संबंधित मुद्दे

 संबंधित मुद्दे