# [Android] Make your app appear in the system share list

> First, in AndroidManifest, you need to add a filter\ , for the Activity in your app that needs to receive shares, next is the Java code control in the Activity, official detailed documentation Google Developer – Detailed explanation of Intent and intent filter Google Devel…

- Published: 2018-01-09
- Tags: Android, Android Development
- View HTML version: https://bilibili.cat/en/posts/android-sys-share
- Translated by AI from 中文: https://bilibili.cat/posts/android-sys-share

---

## First, in AndroidManifest, you need to add a filter\<intent-filter> for the Activity in your app that needs to receive shares,

```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>
```

## Next is the Java code control in the Activity,

```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);
    }
}
```

## Official detailed documentation

[Google Developer – Detailed explanation of Intent and intent-filter](https://developer.android.google.cn/training/sharing/receive.html?hl=zh-cn)  
[Google Developer – Receiving Simple Data from Other Apps](https://developer.android.google.cn/training/sharing/receive.html?hl=zh-cn)
