Puneet Chugh
Puneet Chugh

Reputation: 93

Receiving an Intent when app is running in background(service app)

I am working on an Android app which will be running on a background thread(Service app). I want to receive some data from another app through Intent(ACTION_SEND) from another app.

Is it possible for the app to receive the intent when the app is in background thread.

Upvotes: 2

Views: 2431

Answers (2)

k3b
k3b

Reputation: 14755

You can implement an Activity with no gui of it-s own that gets started when receiving an "ACTION_SEND intent". The activity can then inform your running background-service:

manifest

<manifest ...>
    <application ...>
        <activity android:name=".MyActionSendReceiver">

            <intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- file must have mimeType to match -->
                <data android:mimeType="*/*" />
                <data android:scheme="file" />
            </intent-filter>

        </activity>
    </application>
</manifest>

public class MyActionSendReceiver extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean canWrite = SettingsImpl.init(this);

        Intent mySendIntent = getIntent();

        // TODO inform my running background service

        // no gui has been created. finish this activity
        this.finish();
    }
}

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006614

I am working on an Android app which will be running on a background thread(Service app).

An app does not run on a background thread. An app might have a background thread.

I want to receive some data from another app through Intent(ACTION_SEND) from another app.

ACTION_SEND will only route to an activity of yours. Your activity is welcome to do something involving a background thread at this point. If you use Theme.Translucent.NoTitleBar for the activity theme and call finish() inside onCreate(), you can have the activity have no UI. So, from the user's standpoint, it appears as though ACTION_SEND is being processed in the background, even though this is not actually what is happening.

Upvotes: 1

Related Questions