分享按钮在 android 中不起作用
Share button doesn't work in android
我有 ListView 并且在它的 BaseAdapter 中我想要一个弹出菜单,它的项目之一是 "share" 项目所以当用户点击它时,共享 window/dialog 会弹出:
这是我的弹出菜单是ListView的BaseAdapter:
mViewHolder.optionMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(context, mViewHolder.optionMenuButton);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.share_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle() == "share") {
if (null == mainActivity) {
mainActivity = (MainActivity) context;
}
mainActivity.shareAction();
return true;
}
return false;
}
});
popup.show();//showing popup menu
}
});
这就是我尝试打开共享 window/dialog 的方式,它没有打开共享 window/dialog,但祝酒词说我点击了 "share" 项显示弹出菜单:
public void shareAction() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "You have to check this out: " + "https://www.google.com/";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this out");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
ShareAction()方法在mainActivity中,adapter是ListView的BaseAdapter,这个ListView在MainActivity的一个fragments里面。
我还尝试了我创建的 ShareAction() 方法的这个执行代码,它在没有碎片的 activity 中工作得很好,这就是为什么它在这里不起作用的原因。 .
试试这个以显示共享对话框
public void shareApp(Context context)
{
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out yourAppName at: https://play.google.com/store/apps/details?id=" + appPackageName + refercode);//If you have refer code you can give
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, "Share with..."));
}
从您的编码中删除此代码,
if (null == mainActivity) {
mainActivity = (MainActivity) context;
}
并匹配这样的条件,
if (item.getTitle().equals( "share")) {
mainActivity.shareAction();
return true;
}
我有 ListView 并且在它的 BaseAdapter 中我想要一个弹出菜单,它的项目之一是 "share" 项目所以当用户点击它时,共享 window/dialog 会弹出:
这是我的弹出菜单是ListView的BaseAdapter:
mViewHolder.optionMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(context, mViewHolder.optionMenuButton);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.share_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle() == "share") {
if (null == mainActivity) {
mainActivity = (MainActivity) context;
}
mainActivity.shareAction();
return true;
}
return false;
}
});
popup.show();//showing popup menu
}
});
这就是我尝试打开共享 window/dialog 的方式,它没有打开共享 window/dialog,但祝酒词说我点击了 "share" 项显示弹出菜单:
public void shareAction() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "You have to check this out: " + "https://www.google.com/";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this out");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
ShareAction()方法在mainActivity中,adapter是ListView的BaseAdapter,这个ListView在MainActivity的一个fragments里面。
我还尝试了我创建的 ShareAction() 方法的这个执行代码,它在没有碎片的 activity 中工作得很好,这就是为什么它在这里不起作用的原因。 .
试试这个以显示共享对话框
public void shareApp(Context context)
{
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out yourAppName at: https://play.google.com/store/apps/details?id=" + appPackageName + refercode);//If you have refer code you can give
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, "Share with..."));
}
从您的编码中删除此代码,
if (null == mainActivity) {
mainActivity = (MainActivity) context;
}
并匹配这样的条件,
if (item.getTitle().equals( "share")) {
mainActivity.shareAction();
return true;
}