Android ShareActionProvider在调试模式下工作,但不在实际设备中

Mic*_*nno 4 android android-actionbar shareactionprovider android-actionbar-compat

当我在设备上使用Eclipse调试我的应用程序时,这就是结果,这就是我想要的:

来自调试模式的App图像

'分享图标'工作正常!

在这里,问题!!!! 当我在谷歌播放时添加应用程序,然后我从它安装应用程序,这是结果:

从Google Play安装后的应用图片

'分享图标'不起作用.它是可点击的,但它什么也没做.图标没有出现,只有标题:"分享"

这是代码:

private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
    mShareActionProvider.setShareIntent(getDefaultShareIntent());  
    return super.onCreateOptionsMenu(menu);
}

private Intent getDefaultShareIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.app_play_address));
    return intent;
}
Run Code Online (Sandbox Code Playgroud)

这是main.xml文件:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourapp="http://schemas.android.com/apk/res-auto">

<item 
    android:id="@+id/action_share"
    android:title="@string/share"
    yourapp:showAsAction="ifRoom"
    yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

</menu>
Run Code Online (Sandbox Code Playgroud)

在styles.xml文件中:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
Run Code Online (Sandbox Code Playgroud)

v-11中的styles.xml文件:

<resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <!-- API 11 theme customizations can go here. -->
</style>
Run Code Online (Sandbox Code Playgroud)

v-14中的styles.xml文件:

<resources>

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <!-- API 14 theme customizations can go here. -->
</style>
Run Code Online (Sandbox Code Playgroud)

最后,在manifest.xml上:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
Run Code Online (Sandbox Code Playgroud)

arn*_*ans 13

您是否在使用Proguard进行发布?

我这样做了,并且遇到了同样的问题,直到我意识到这个类ShareActionProvider被Proguard破坏了,因此我不得不把它从Proguard-mangling中取出来.

因此,在您的proguard-project.txt中,包含以下行:

-keep class android.support.v7.widget.ShareActionProvider { *; }
Run Code Online (Sandbox Code Playgroud)

  • 现在,随着 AndroidX 取代 Support,它将是: -keep class androidx.appcompat.widget.ShareActionProvider { *; } (2认同)