0

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

Ajay
  • 403
  • 2
  • 5
  • 13
  • why not use BroadCastReceiver in Service ? – Cgx Jul 15 '16 at 14:47
  • I can use broadcast receiver. can you please elaborate how would that solve my problem? – Ajay Jul 15 '16 at 14:49
  • My understanding is you want your Service to receive IntentFilters from other apps that want to share data? – DeeV Jul 15 '16 at 14:53
  • yes that is exactly what i want. – Ajay Jul 15 '16 at 14:54
  • 1
    You've set your Activity to receive those same IntentFilters? So your Activity is catching them. Why not dynamically register them when your Activity is open/started, and unregister them when it is closed/when you don't want it to receive the Broadcast? – Mark Jul 15 '16 at 14:58
  • Sorry. I believe this is what you want. http://stackoverflow.com/questions/15071830/external-service-can-not-be-resolved-by-intent-filter – DeeV Jul 15 '16 at 14:59
  • @MarkKeen if i remove intent filter from activity then i dont see share button for my application in other application. Are you telling me unregister them programmatically when application is launched? – Ajay Jul 15 '16 at 15:11
  • The system "share to" button requires an Activity. The only thing you could do is launch a transparent Activity that starts the Service which retrieves the data you need, but the Activity will have to live as long as it's waiting for data to send back to the calling Activity. – DeeV Jul 15 '16 at 15:16
  • You cannot start a service with those intentFilters. Create a Dummy Activity to intercept those IntentFilters, and call `startService()` in `onCreate`, then `finish();` – Mark Jul 15 '16 at 15:16
  • An option would be to use a 'IntentFilter' Activity (Dummy Activity) that handles all Filters and then decides what to do with it from their (I don't know what your use cases are for starting the MainActivity or your Service) ... maybe a SharedPreferences boolean that you update as needed to check which should be used.. – Mark Jul 15 '16 at 15:25
  • I tired that, finish(); activity closes and my service is running. But actually I need to pass data to service quite often. So everytime I need to pass data from other apps , Would I need to open the activity ? because as of now if i try to pass data it open the activity again. – Ajay Jul 15 '16 at 15:29
  • So My service is like a service that needs data from other apps quite often, for example i need to get images URI from images in the gallery to the service. – Ajay Jul 15 '16 at 15:32
  • Are you using `START_STICKY` in your Service? – Mark Jul 15 '16 at 15:38
  • No How would that be helpful to my problem ? – Ajay Jul 15 '16 at 15:41
  • How you implement your Service is up to you. I'm trying to provide suggestions on how to solve your posted question which was to do with Intent Filters and a Service. – Mark Jul 15 '16 at 16:18
  • Thanks I appreciate for all the suggestion. – Ajay Jul 15 '16 at 18:42

0 Answers0