[android] Broadcast Receiver within a Service

I am trying to start up a BroadcastReceiver within a Service. What I am trying to do is have a background running service going that collects incoming text messages, and logs incoming phone calls. I figured the best way to go about this is to have a service running that incorporates a broadcast receiver that can catalog either.

How do i go about doing this? I already have my service up and running.

This question is related to android sms broadcastreceiver android-service

The answer is


The better pattern is to create a standalone BroadcastReceiver. This insures that your app can respond to the broadcast, whether or not the Service is running. In fact, using this pattern may remove the need for a constant-running Service altogether.

Register the BroadcastReceiver in your Manifest, and create a separate class/file for it.

Eg:

<receiver android:name=".FooReceiver" >
    <intent-filter >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

When the receiver runs, you simply pass an Intent (Bundle) to the Service, and respond to it in onStartCommand().

Eg:

public class FooReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do your work quickly!
        // then call context.startService();
    }   
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to sms

Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4) Android: Share plain text using intent (to all messaging apps) Send a SMS via intent Broadcast Receiver within a Service How to get javax.comm API? Android – Listen For Incoming SMS Messages How to pre-populate the sms body text via an html link Show compose SMS view in Android How to access the SMS storage on Android? Android - SMS Broadcast receiver

Examples related to broadcastreceiver

Sending intent to BroadcastReceiver from adb Broadcast receiver for checking internet connection in android app Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4) android - How to get view from context? Broadcast Receiver within a Service How to use LocalBroadcastManager? Android - Start service on boot Get Context in a Service How do I start my app on startup? Receiver not registered exception error?

Examples related to android-service

Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent Android Fatal signal 11 (SIGSEGV) at 0x636f7d89 (code=1). How can it be tracked down? How to get the current location latitude and longitude in android Service vs IntentService in the Android platform Background service with location listener in android Launch Android application without main Activity and start Service on launching application START_STICKY and START_NOT_STICKY Broadcast Receiver within a Service How to run an android app in background? Android background music service