Ajay
Ajay

Reputation: 523

Can we receive data from other apps in a Background Service?

according to android documentation we can definitely receive data from other apps in the activity as shown in the link.

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

So I tried this and it works perfectly, But I have special where I don't want the activity to come to foreground. I want the intent to be received by the service instead of the activity.

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

But somehow I cannot receive intent in service, as it seems android:launchMode=singleTask is valid only for activities.

OnNewIntent works fine for activity as shown below

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

Can someone suggest way in which i can receive intent/data from other apps in the service. ?

Upvotes: 0

Views: 967

Answers (1)

Asghar Hosseini
Asghar Hosseini

Reputation: 297

In my scenario when android:launchMode="singleTask" I added it in the manifest file and the problem was solved

    <activity
        android:name=".ui.IActivity"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" />

Upvotes: 0

Related Questions