我们可以在后台服务中接收来自其他应用程序的数据吗?

问题描述 投票:0回答:1

根据android文档,我们绝对可以从活动中的其他应用程序接收数据,如链接所示。

https://developer.android.com/training/sharing/receive.html

所以我尝试了这个,它工作得很好,但我有一个特殊的地方,我不希望该活动进入前台。我希望服务而不是活动接收意图。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />



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

 <service android:name=".BackgroundPresentation"
          android:exported="false">

  <intent-filter>
      <action android:name="android.intent.action.SEND" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="image/*" />
    </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>

   <intent-filter>
       <action android:name="android.intent.action.SEND_MULTIPLE" />
       <category android:name="android.intent.category.DEFAULT" />
       <data android:mimeType="image/*" />
   </intent-filter>

  </service>



  <activity android:name=".MainActivity"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden">

     <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="image/*" />
      </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>

       <intent-filter>
          <action android:name="android.intent.action.SEND_MULTIPLE" />
          <category android:name="android.intent.category.DEFAULT" />
           <data android:mimeType="image/*" />
       </intent-filter>
    </activity>
</application>

但不知怎的,我无法接收服务中的意图,因为似乎 android:launchMode=singleTask 仅对活动有效。

OnNewIntent 对于活动效果很好,如下所示

protected void onNewIntent(Intent intent) {
    Log.e(TAG, "------->NewIntent is also called");
    super.onNewIntent(intent);
    setIntent(intent);//must store the new intent unless getIntent() will return the old one

    // Get intent, action and MIME type
    String action = intent.getAction();
    String type = intent.getType();

    Log.d(TAG, "onCreate: intent is " +    intent.getStringExtra(Intent.EXTRA_TEXT));
}

有人可以建议我从服务中的其他应用程序接收意图/数据的方式吗? ?

android android-intent android-service android-intentservice android-service-binding
1个回答
0
投票

在我的场景中,当 android:launchMode="singleTask" 时 我将其添加到清单文件中,问题就解决了

    <activity
        android:name=".ui.IActivity"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" />
© www.soinside.com 2019 - 2024. All rights reserved.