سؤال

I am trying to implement the Share icon along with my overflow menu on the app. But the share icon is gray and I am unable to click on it.

My menu XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:icon="@drawable/ic_action_share"
        android:actionProviderClass="android.widget.ShareActionProvider" />
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="About"/>
    <item
        android:id="@+id/action_help"
        android:orderInCategory="200"
        android:showAsAction="never"
        android:title="Help"/>
    <item
        android:id="@+id/action_rate"
        android:orderInCategory="300"
        android:showAsAction="never"
        android:title="Rate"/>
</menu>

My menu Java code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem item = menu.findItem(R.id.menu_item_share);

        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
        String strMsg = "Blood Pressure is: \t\t" + tvSVal.getText().toString() + "/" + tvDVal.getText().toString();

        Intent myIntent5 = new Intent();
        myIntent5.setAction(Intent.ACTION_SEND);
        myIntent5.putExtra(Intent.EXTRA_TEXT, strMsg);
        myIntent5.setType("text/plain");

        mShareActionProvider.setShareIntent(myIntent5);

        return true;
    }

    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_help:
            //display the help activity
            Intent myIntent2 = new Intent(getApplicationContext(), HelpFile.class);
            startActivityForResult(myIntent2, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_about:
            //display the about window
            Log.i("Opening About Activity", "ABOUT");
            Intent myIntent = new Intent(MainActivity.this, AboutApp.class);
            startActivityForResult(myIntent, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_rate:
            //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_VIEW);
            //Try Google play
            intent.setData(Uri.parse("market://details?id=com.test.testing"));
            if (MyStartActivity(intent) == false) {
                //Market (Google play) app seems not installed, let's try to open a web browser
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?com.test.testing"));
                if (MyStartActivity(intent) == false) {
                    //Well if this also fails, we have run out of options, inform the user.
                    //let the user know nothing was successful
                }
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    private boolean MyStartActivity(Intent aIntent) {
        try
        {
            startActivity(aIntent);
            return true;
        }
        catch (ActivityNotFoundException e)
        {
            return false;
        }
    }

I get the following warning: The method setShareIntent(Intent) from the type MainActivity is never used locally

Please help me resolve it.

هل كانت مفيدة؟

المحلول

You need to set the intent of the ShareActionProvider. When creating your menu, save an instance of the ShareActionProvider from the menu. Either at that point or later on, attach an intent to it via setShareIntent(...). For a more detailed explanation and an example, see the Google documentation here: http://developer.android.com/training/sharing/shareaction.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top