gpt4 book ai didi

java - Android V7 ShareActionProvider 分享按钮不工作

转载 作者:行者123 更新时间:2023-11-30 10:57:39 24 4
gpt4 key购买 nike

我在使用 ShareActionProvider 时遇到了一个奇怪的问题。每件事都正确链接。分享按钮在那里,但它不起作用。我确实在 logcat 上看到了一个事件,但在电话上没有任何反应。

菜单.xml

<xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_share"
android:title="@string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>

fragment 菜单初始化代码

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;

public DetailsFragment() {
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.detailsfragment_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// If onLoadFinished happens before this, we can go ahead and set the share intent now.
if (YOUTUBE_KEY != null) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
}
}

//分享 Intent 代码

 private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
Uri videoLocation = Uri.parse(YOUTUBE_API).buildUpon()
.appendQueryParameter("v", YOUTUBE_KEY)
.build();
shareIntent.putExtra(Intent.EXTRA_TEXT, MOVIES_SHARE_HASHTAG);
shareIntent.setData(videoLocation);

return shareIntent;
}

最佳答案

使用 ShareActionProvider 它需要在初始化后立即设置内容,所以在你的 onCreateOptionMenu 中使用虚拟内容进行共享,并在初始化后将 ShareProvider 的引用获取到全局 ShareActionProvider 对象中,即 globalShareActionProvider

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);

// Get the provider and hold onto it to set/change the share intent
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

// Attach an intent ot this ShareActionProvider.
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
globalShareActionProvider=mShareActionProvider;
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}

在此之后,调用 setShareIntent 以创建具有更新值的新 Intent

就像我在我的 onLoaderFinish 函数中使用它一样

        shareActionString= String.format("%s - %s - %s/%s",
mdateView.getText(), mNewShareContent, hour, minutes);
globalShareActionProvider.setShareIntent(createShareForecastIntent());

我的 createShareForecastIntent()

private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, shareActionString
+ SHARE_HASHTAG);
return shareIntent;
}

关于java - Android V7 ShareActionProvider 分享按钮不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32526777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com