2016-09-16 7 views
0

GovDicという名前のアプリケーションで共有機能を作成しました。しかし、シェアリストでは、アプリケーション自体が現れました。それをシェアから除外する方法は?Android共有(ShareActionProvider)からアプリケーション自体を除外する方法

私は共有する必要がありますtext/plain他のアプリケーションへのコンテンツ、また他のアプリはこのアプリケーションにtext/plainコンテンツを共有することができます。

マニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.comp548.govdic"> 

    ... 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     ... 

     <activity android:name=".OrgDetailActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 

      <meta-data android:name="android.support.PARENT_ACTIVITY" 
       android:value=".MainActivity"/> 
     </activity> 
    </application> 

</manifest> 

メニュー:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

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

アクティビティ:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate menu resource file. 
    getMenuInflater().inflate(R.menu.detail_menu, menu); 

    // Locate MenuItem with ShareActionProvider 
    MenuItem menuItem = menu.findItem(R.id.menu_item_share); 

    // Fetch and store ShareActionProvider 
    ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 

    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType(Constant.MIME_TYPE_TEXT_PLAIN); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, etDescription.getText().toString()); 

    shareActionProvider.setShareIntent(shareIntent); 

    // Return true to display menu 
    return true; 
} 

enter image description here

答えて

0

これらの関数は、あなたが欲しい達成するのを助けることができます。ちょうどあなたのパッケージ名を渡し

public static void shareExludingApp(Context ctx, String packageNameToExclude, android.net.Uri imagePath, String text) { 

     List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
     Intent share = new Intent(android.content.Intent.ACTION_SEND); 
     share.setType("image/*"); 
     File file = new File(imagePath.getPath()); 
     List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(text, file), 0); 
     if (!resInfo.isEmpty()) { 
      for (ResolveInfo info : resInfo) { 
       Intent targetedShare = createShareIntent(text, file); 

       if (!info.activityInfo.packageName.equalsIgnoreCase(packageNameToExclude)) { 
        targetedShare.setPackage(info.activityInfo.packageName); 
        targetedShareIntents.add(targetedShare); 
       } 
      } 

      Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), 
        "Select app to share"); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
        targetedShareIntents.toArray(new Parcelable[]{})); 
      ctx.startActivity(chooserIntent); 
     } 

    } 

    private static Intent createShareIntent(String text, File file) { 
     Intent share = new Intent(android.content.Intent.ACTION_SEND); 
     share.setType("image/*"); 
     if (text != null) { 
      share.putExtra(Intent.EXTRA_TEXT, text); 
     } 
     share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
     return share; 
    } 
2

こんにちは@のuser2299040と@Marcin Orlowski:

メニュー:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item android:id="@+id/menu_item_share" 
     app:showAsAction="ifRoom" 
     android:icon="@drawable/ic_share" 
     android:title="@string/share" /> 
</menu> 

活動:

は、私はいくつかのコードを修正し、それが働いた、ありがとう

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate menu resource file. 
    getMenuInflater().inflate(R.menu.detail_menu, menu); 
    // Return true to display menu 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case R.id.menu_item_share: 
      Common.shareExcludingApp(this, "com.comp548.govdic", etDescription.getText().toString()); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

シェア機能:

public static void shareExcludingApp(Context ctx, String packageNameToExclude, String text) { 

    List<Intent> targetedShareIntents = new ArrayList<>(); 
    Intent share = new Intent(android.content.Intent.ACTION_SEND); 
    share.setType(Constant.MIME_TYPE_TEXT_PLAIN); 
    List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(text), 0); 
    if (!resInfo.isEmpty()) { 
     for (ResolveInfo info : resInfo) { 
      Intent targetedShare = createShareIntent(text); 

      if (!info.activityInfo.packageName.equalsIgnoreCase(packageNameToExclude)) { 
       targetedShare.setPackage(info.activityInfo.packageName); 
       targetedShareIntents.add(targetedShare); 
      } 
     } 

     Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), 
       "Select app to share"); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
       targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()])); 
     ctx.startActivity(chooserIntent); 
    } 

} 

private static Intent createShareIntent(String text) { 
    Intent share = new Intent(android.content.Intent.ACTION_SEND); 
    share.setType(Constant.MIME_TYPE_TEXT_PLAIN); 
    if (text != null) { 
     share.putExtra(Intent.EXTRA_TEXT, text); 
    } 
    return share; 
} 
関連する問題