【问题标题】:Receiving Simple Data Through Intent for Android App while App is in background应用在后台时通过 Intent 为 Android 应用接收简单数据
【发布时间】:2018-01-27 04:03:19
【问题描述】:

我有一个应用程序通过其他应用程序共享到我的应用程序接收来自其他应用程序的简单数据(文本)。部分清单如下所示:

    <activity
        android:name=".presentation.ui.activities.MainActivity"
        android:launchMode="singleTop">
        <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" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>

当 Activity 尚未创建时,我将来自其他应用的文本数据共享给我的应用,并在 onCreate 方法中使用动作 intent.action.Send 处理意图。但是,我还希望能够在应用程序处于后台时共享文本数据。当应用程序在后台并且我从其他应用程序共享文本数据时,操作是 onRestart 或 onResume 方法中的 intent.action.MAIN。

这是handleIntent方法

private void handleIntent(){
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            String text = intent.getStringExtra(Intent.EXTRA_TEXT);
            // logic
        }
    }
}

当我的应用在后台时,如何从其他应用接收文本数据?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    我想通了。

    我覆盖了 newIntent 方法

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }
    

    【讨论】:

      【解决方案2】:

      向其他应用发送简单数据

      Intent sendIntent = new Intent();
      sendIntent.setAction(Intent.ACTION_SEND);
      sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
      sendIntent.setType("text/plain");
      startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
      

      从其他应用接收简单数据

      更新您的清单

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

      处理传入的内容

       void onCreate (Bundle savedInstanceState) {
      
          // Get intent, action and MIME type
          Intent intent = getIntent();
          String action = intent.getAction();
          String type = intent.getType();
      
          if (Intent.ACTION_SEND.equals(action) && type != null) {
              if ("text/plain".equals(type)) {
                  handleSendText(intent); // Handle text being sent
              }
          }
      }
      
      void handleSendText(Intent intent) {
          String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
          if (sharedText != null) {
              // Update UI to reflect text being shared
          }
      }
      

      【讨论】:

      • 将活动带回前台时不会调用 onCreate 方法。发送的意图是intent.action.Main。
      • 你需要刷新活动
      猜你喜欢
      • 2021-06-30
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多