Java 自定义共享意图对话框是否可能?
开放共享意图代码Java 自定义共享意图对话框是否可能?,java,android,android-intent,Java,Android,Android Intent,开放共享意图代码 String xtype = "image/*"; Intent share = new Intent(Intent.ACTION_SEND); share.setType(xtype); Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(tmpImageUri.getPath())); share.putExtra(Intent.E
String xtype = "image/*";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(xtype);
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(tmpImageUri.getPath()));
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share via"));
它显示如下共享意图对话框
但现在我想要这样
可以自定义共享意图对话框吗?可以,可以创建自定义共享对话框。您只需获取
意向性
列表,并根据需要进行定制。对于示例,您可以按以下操作
步骤1:准备意图
步骤2:获取活动列表并设置
步骤3:在AlertDialog中设置该适配器
输出:-
它的自定义可共享对话框使用,但您可以根据需要使用自定义布局和创建类来进行所有设置(UI、选择、主题等)是的,请检查,您将获得一个应用程序列表,然后以任何方式使用它to@RaviRupareliya感谢您的回复,先生,但不要列出接受图像的应用程序列表我想定制共享意图对话框任何想法我如何实现这一点使用此代码您将获得所有可以共享数据的应用程序,在您的自定义列表中使用该列表,就这样。@RaviRupareliya这是个好主意,谢谢您这样做help@Ashvinsolanki那么...怎么样
String urlToShare = "https://play.google.com/store/apps/details?id=com.yourapp.packagename";
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "If any extra"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application \n\n" + urlToShare);
final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
List<DialogItem> appNames = new ArrayList<DialogItem>();
for (ResolveInfo info : activities) {
appNames.add(new DialogItem(info.loadLabel(getPackageManager()).toString(),
info.loadIcon(getPackageManager())));
}
final List<DialogItem> newItem = appNames;
ListAdapter adapter = new ArrayAdapter<DialogItem>(activity,
android.R.layout.select_dialog_item, android.R.id.text1, newItem) {
public View getView(int position, View convertView, ViewGroup parent) {
//Use super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = v.findViewById(android.R.id.text1);
tv.setText(newItem.get(position).app);
tv.setTextSize(15.0f);
//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(newItem.get(position).icon, null, null, null);
//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);
return v;
}
};
public class DialogItem {
public String app = "";
public Drawable icon;
public DialogItem(String name, Drawable drawable) {
app = name;
icon = drawable;
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Custom Sharing Dialog");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.equals("com.facebook.katana")) {
Toast.makeText(activity, "Facebook Selected ", Toast.LENGTH_LONG).show();
} else {
// start the selected activity
Log.i(TAG, "Hi..hello. Intent is selected");
intent.setPackage(info.activityInfo.packageName);
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();