# [Android] アプリをシステムの共有リストに表示させる

> まず、AndroidManifest内で、あなたのアプリのうち共有を受け取る必要があるActivityにフィルターを追加する必要があります\ , 次にActivity内のJavaコードによる制御です, 公式の詳細ドキュメント Google Developer – Intentとintent filterの詳細 Google Devel…

- 公開日: 2018-01-09
- タグ: Android, Android開発
- HTML版を見る: https://bilibili.cat/ja/posts/android-sys-share
- 中文からAI翻訳: https://bilibili.cat/posts/android-sys-share

---

## まず、AndroidManifest内で、あなたのアプリのうち共有を受け取る必要があるActivityにフィルターを追加する必要があります\<intent-filter>,

```xml
<activity
 android:name="com.xxx.MainActivity"
 android:label="@string/app_name"
 android:theme="@style/AppTheme.NoActionBar"
 android:screenOrientation="portrait">
  
<!-- 默认的过滤器 -->
<intent-filter>
 <action android:name="android.intent.action.MAIN" />
<category  android:name="android.intent.category.LAUNCHER" />
</intent-filter>

 <!-- 添加接受分享指令的过滤器 -->
 <intent-filter>
 <action android:name="android.intent.action.SEND" />
 <category android:name="android.intent.category.DEFAULT" />
 <!-- 指定分享类型（mimetype）为纯文本 -->
 <data android:mimeType="text/plain" />
 </intent-filter>

</activity>
```

## 次に、Activity内のJavaコードによる制御です,

```Java
//写一个初始化函数init()放在onCreate()里
//init()里写一个try catch接收分享的intent
//使用try catch 防止intent传入为空时崩溃
try {
     Intent intent = getIntent();
     if (intent.getType().equals("text/plain")) {
          handleSendText(intent); // Handle text being sent
         }
     } catch (Exception e) {
      e.printStackTrace();
      Log.d("Error", "noIntent");
     }

//处理接收的分享数据
public void handleSendText(Intent intent) {
 String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
 if (sharedText != null) {
       String id = sharedText.substring(sharedText.indexOf("xx"), sharedText.indexOf("xx"));
//从字符串里取出id
      editText.setText(id);
    }
}
```

## 公式の詳細ドキュメント

[Google Developer – Intentとintent-filterの詳細](https://developer.android.google.cn/training/sharing/receive.html?hl=zh-cn)  
[Google Developer – 他のアプリからシンプルなデータを受け取る](https://developer.android.google.cn/training/sharing/receive.html?hl=zh-cn)
