2011-09-21 3 views
19

빨리 뭔가를 공유하기 위해 Android Intent.ACTION_SEND를 사용하고 싶습니다. 그래서 나는이 같은 공유 목록을 가지고 : Sharing intent listIntent.ACTION_SEND에서 어떤 인 텐트가 선택되었는지 어떻게 알 수 있습니까?

을하지만 같은 각 작업에 대해 서로 다른 컨텐츠를 공유하려는 :

  • 하는 이메일/Gmail에 의해 공유, 내용이 "이메일로 공유 할 필요가있는 경우 ".

  • Facebook에서 공유하는 경우 콘텐츠는 "Facebook에서 공유"해야합니다.

이렇게 할 수 있습니까?

+0

문제가 있습니까? 클릭 한 항목에 따라 의도를 보냅니 까?! 문제가있는 곳은 –

+0

아, 공유 작업을 수행하기 위해 어떤 인 텐트가 선택되었는지 어떻게 알 수 있습니까? – anticafe

+0

http://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser 및 http://stackoverflow.com/questions/4417019/에 따르면 어떻게 사용자 선택을 시작에서부터 시작하여 사용자가 원하는대로 만들지는 알 수 없습니다. – anticafe

답변

26

이러한 정보를 얻을 수 없습니다.

활동 선택 대화 상자를 직접 구현하지 않는 한.

이러한 대화 상자를 만들려면 PackageManager 및 해당 queryIntentActivities() 기능을 사용해야합니다. 이 함수는 List<ResolveInfo>을 반환합니다.

ResolveInfo

는 활동 ( resolveInfo.activityInfo.packageName)에 대한 몇 가지 정보를 포함하고 PackageManager의 도움으로 있습니다 (대화 상자에서 활동을 표시하는 데 유용합니다) 다른 정보를 얻을 수 있습니다 - 응용 프로그램 아이콘 당김, 응용 프로그램 라벨, ...을.

결과를 대화 상자 (또는 대화 상자 스타일의 활동)에 목록으로 표시하십시오. 항목을 클릭하면 새 Intent.ACTION_SEND를 만들고 원하는 내용을 추가하고 선택한 활동의 ​​패키지를 추가하십시오 (intent.setPackage(pkgName)).

5

답을 찾고있는 중인지 확실하지 않은 경우 ClickClickClack에는 ACTION_SEND 인 텐트를 가로채는 방법과 패키지 이름 및 특정 특성을 기반으로 일어나는 일을 실제로 구현하는 방법이 나와 있습니다. In은 Tomik이 언급 한 대부분의 단계를 포함합니다. 이 구현에 대한

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 단계 : 지금 당신은 (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 레이아웃 타이 만들기 pe 대화 상자에서 목록보기를 표시합니다 (social_share.XML)

<?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.show()를 변경하는 것이 좋습니다. if (! alert.isShowing()) {alert.show(); } – jmnwong

1

내가 패키지 매니저 loadLabel와 LoadIcon 사용하여 내 자신의 사용자 정의 공유 목록 생성 할 수있어 Tomik 큰 응답을 사용하여 :

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())); 
     } 
    } 

} 
관련 문제