2011-09-21 24 views
19

Tôi muốn sử dụng Android Intent.ACTION_SEND để nhanh chóng chia sẻ điều gì đó. Vì vậy, tôi có một danh sách chia sẻ như thế này: Sharing intent listCách biết ý định nào được chọn trong Intent.ACTION_SEND?

Nhưng tôi muốn chia sẻ nội dung khác nhau cho mỗi hành động, chẳng hạn như:

  • Nếu chia sẻ qua Email/Gmail, nội dung cần được "Chia sẻ qua email ".

  • Nếu chia sẻ bằng Facebook, nội dung phải là "Chia sẻ bởi Facebook".

Vì vậy, có thể thực hiện điều đó không?

+0

đâu là vấn đề? bạn gửi ý định tùy thuộc vào mục được nhấp ?! đâu là vấn đề –

+0

à, ý tôi là làm sao tôi biết được Intent nào được chọn để thực hiện hành động chia sẻ? – anticafe

+0

quá tệ, theo http://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser và http://stackoverflow.com/questions/4417019/ làm thế nào để có được-người-lựa chọn-từ-startactivityforresultintent-createchooserfi, tôi không thể tìm thấy những gì người dùng ý định chọn. – anticafe

Trả lời

26

Bạn không thể nhận được thông tin như vậy.

Trừ khi bạn tạo triển khai hộp thoại của riêng bạn cho lựa chọn hoạt động.

Để tạo hộp thoại như vậy, bạn cần sử dụng PackageManager và chức năng queryIntentActivities(). Hàm trả về List<ResolveInfo>.

ResolveInfo chứa một số thông tin về hoạt động (resolveInfo.activityInfo.packageName) và với sự trợ giúp của PackageManager bạn có thể nhận thông tin khác (hữu ích để hiển thị hoạt động trong hộp thoại) - biểu tượng ứng dụng có thể vẽ, nhãn ứng dụng, ....

Hiển thị kết quả trong danh sách trong hộp thoại (hoặc trong một hoạt động được tạo kiểu dưới dạng hộp thoại). Khi một mục được nhấp, hãy tạo Intent.ACTION_SEND mới, thêm nội dung bạn muốn và thêm gói của hoạt động đã chọn (intent.setPackage(pkgName)).

5

Không chắc chắn nếu bạn vẫn đang tìm kiếm câu trả lời, nhưng ClickClickClack có ví dụ về cách bạn có thể chặn ý định ACTION_SEND và chọn dựa trên tên gói và một số đặc điểm nhất định. Trong hầu hết các bước được đề cập bởi Tomik.

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

Một khía cạnh mạnh mẽ về việc triển khai này là bạn có thể thêm phân tích vào cuộc gọi của mình.

+0

đây là một triển khai tuyệt vời. – TomaszRykala

11

Không có phương pháp trực tiếp truy cập vào loại đó của thông tin ....

Bước 1: Bên trong mã của bạn trước hết bạn cần phải khai báo một bộ chuyển đổi mà sẽ chứa giao diện tùy chỉnh của bạn danh sách để được chia sẻ trên ...

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

Bước 2: Bây giờ bạn đã tạo ra một inflater bố trí cho bộ chuyển đổi của bạn (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 

Bước 3: tạo xml của bạn bố trí ty pe để hiển thị chế độ xem danh sách trong hộp thoại (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

Tôi cũng khuyên bạn nên thay đổi alert.show() để được bao quanh bởi một IF để ngăn chặn nhiều lần nhấp chuột. if (! alert.isShowing()) {alert.show(); } – jmnwong

1

Sử dụng Tomik trả lời lớn tôi có thể sản xuất Tuỳ chỉnh Danh sách cổ phiếu của riêng tôi sử dụng PackageManager loadLabel và 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())); 
     } 
    } 

} 
Các vấn đề liên quan