Broadcast Receiver within a Service
as your service is already setup, simply add a broadcast receiver in your service:
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
}
else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
//action for phone state changed
}
}
};
in your service's onCreate
do this:
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction("your_action_strings"); //further more
filter.addAction("your_action_strings"); //further more
registerReceiver(receiver, filter);
and in your service's onDestroy
:
unregisterReceiver(receiver);
and you are good to go to receive broadcast for what ever filters you mention in onCreate
. Make sure to add any permission if required. for e.g.
<uses-permission android:name="android.permission.RECEIVE_SMS" />
How do I implement a BroadcastReceiver in a Service Class in Android?
Have the BroadcastReceiver as a top-level class or as an inner class in your service. And get a reference of the receiver in your service. Like this:
public class MyService extends Service {
BroadcastReceiver mReceiver;
// use this as an inner class like here or as a top-level class
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// do something
}
// constructor
public MyReceiver(){
}
}
@Override
public void onCreate() {
// get an instance of the receiver in your service
IntentFilter filter = new IntentFilter();
filter.addAction("action");
filter.addAction("anotherAction");
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
}
}
Android - Service with BroadcastReceiver
You can use this Android service broadcastreceiver example to achieve your goal.
As alternatives you need to provide your Service
class a callback to the MainActivity. You can use Bound services as a starting point.
BroadcastReceiver in Service Android
When your target is to listen for phone state changes in the Service
class itself. You can try to define it internally within the Service
. This will decrease resource usage too as the system is not burdened with handling the lifecycle of another component. In your Service
:
private final PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// Call receive state
if (state != TelephonyManager.CALL_STATE_IDLE) {
// Do something
}
}
};
Then you can register it to listen as (probably in onStartCommand()
):
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
And then unregister it from listening in onDestroy()
(or whenever you are done):
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);
Broadcast receiver inside a Service doesn't work
AlarmReceiver class should be static,
hope it helps.
Implement Broadcast receiver inside a Service
I will show you how to create SMS receiver in a service:
public class MyService extends Service {
@Override
public void onCreate() {
BwlLog.begin(TAG);
super.onCreate();
SMSreceiver mSmsReceiver = new SMSreceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(SMS_RECEIVE_ACTION); // SMS
filter.addAction(WAP_PUSH_RECEIVED_ACTION); // MMS
this.registerReceiver(mSmsReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
/**
* This class used to monitor SMS
*/
class SMSreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (TextUtils.equals(intent.getAction(), SMS_RECEIVE_ACTION)) {
//handle sms receive
}
}
}
passing data from BroadcastReceiver to Service
In receiver, you can just put all params into a Bundle, and use startService(Intent) containing that bundle to call the target service. In service, you parse the bundle and get all params.
Related Topics
How to Set Id of Dynamic Created Layout
Jetpack Compose - Order of Modifiers
Referencing a String in a String Array Resource with Xml
Better Way to Format Currency Input Edittext
Using Asynctask to Load Images in Listview
Using Gson to Parse a JSON with Dynamic "Key" and "Value" in Android
Android Support Repo 46.0.0 with Android Studio 2.3
How to Capture an Image and Store It with the Native Android Camera
How to Keep/Exclude a Particular Package Path When Using Proguard
HTML List Tag Not Working in Android Textview. What How to Do
Transfer Data Between iOS and Android via Bluetooth
How to Send an Email with a File Attachment in Android
Switching Application-Wide Theme Programmatically
Android Dex Gives a Bufferoverflowexception When Building
How to Set Layout_Gravity Programmatically
Android Studio Gradle Configuration with Name 'Default' Not Found