Kotlin - Broadcast Receiver for SmsReceiver issue in Android - android

I am using Broadcast Receive as below :
private val mSmsReceiver=SmsReceiver()
/*
* Default Activity life cycle method which registers Broadcast receiver named
* SmsReceiver for the intent filter SMS_RECEIVED.
* */
public override fun onResume() {
LogUtil.e("<<< onResume MESSAGE", "RECEIVED")
//if(PermissionUtil.hasPermissions(this#OTPUtilityActivity,READ_SMS)){
LocalBroadcastManager.getInstance(this).registerReceiver(
mSmsReceiver,
IntentFilter("android.provider.Telephony.SMS_RECEIVED")
)
super.onResume()
}
I am registering it as above.
Now, I am unregistered it as below when I got success in my web service:
LocalBroadcastManager.getInstance(this).unregisterReceiver(mSmsReceiver)
And yes, I have taken tag for it in manifest as below :
<receiver android:name=".ui.core.broadcast_receiver.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
The Issue is, Broadcast receiver's method also called when I have navigated to another screen on success of my service after unregistering receiver.
It's calling when any new message received which should not call since I have unregistered it.
How can I completely unregister it, so that it never call again when any sms receives in device ?
Note : I have tried removing receiver from Manifest. But, with that I can't receive a singal message.
Thanks.

Remove the intent filter from manifest. If you register, unregister from code, that's not necessary. As it would continue to listen to that broadcast at app level.

Related

My Android broadcast receiver is never triggered

In AndroidManifest.xml I have this:
<receiver android:name=".MyBroadcast" android:exported="true"/>
My broadcast file:
package com.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, Radio.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
I am trying to run application after close it to play music in background.
That's because you never specify what intent you're actually listening for.
You need to register the broadcast receiver to listen for specific broadcasts (events), either in the manifest using the intent-filter tag or dynamically at runtime. See this question for more discussion about the difference.
Here's an example of how to do this in the manifest (from the linked question):
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY"/>
</intent-filter>
</receiver>
This means that the broadcast receiver is listening for the AUDIO_BECOMING_NOISY intent. (You'll want to replace this with a more appropriate intent that reflects when you want this to run).
There's a very useful list of Intents that you can listen for here. You can select a broadcast from there (or from one of the libraries) or, if you're listening for an event that occurs within your application, you can raise the broadcast yourself.
Also, make sure that the event in question is actually being raised. If the broadcast you're listening for never happens, the broadcast receiver will never actually be triggered.
For related reading, see the Observer Pattern (which is the design pattern that Android Broadcast Receivers implement).
onReceive() method is only called when the event you have registered for occurs. You have not declared that event that will trigger the onReceive() method. So, the Broadcast Receiver doesn't know what it should listen for.
You should read more about the Broadcast Receivers and Activity Lifecycle methods from Android Docs.
I don't think you need to use Broadcast Receivers. You can use Activity lifecycle methods to do whatever you want when your application closes.
onReceive() method is only called when the event you have registered for occurs. You have not declared that event that will trigger the onReceive() method. So, the Broadcast Receiver doesn't know what it should listen for.
You should read more about the Broadcast Receivers and Activity Lifecycle methods from Android Docs.
This is similar to asking person X(anyone) to get ________ from the market for you. He is in the market looking for ________ but he does not know what it is. So, obviously, he can't get it for you. You need to tell the receiver what to look for.

Update UI when app is in background. Unregister BroadcastReceiver

I need to update the UI of my fragment when I receive an event. For that I am using LocalBroadCastReceiver. When I receive the event from my server, I set the data in cache and call sendBroadcast. In onReceive of my receiver I am getting the data from cache and updating the UI.
All this works fine until I unregister the receiver in onStop(). So When I put the app in the background, receiver gets unregistered and onReceive method does not get called.
I want to know is it ok if I do not unregister the receiver on onStop ? If it is not recommended then what should I use in place of BroadcastReceiver so that I can update the data even when the app is in backgroud.
Register Broadcast Listener in Manifest file under Application tag:
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="<ACTION_NAME>"/>
</intent-filter>
</receiver>
You will receive onReceive, when the app is in background. Also, you will have to create separate class extending BroadcastReceiver class. And, write the name of your class instead of TestReceiver in manifest file.
public class TestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}}

Different broadcastreceiver depending on activity life

Some time ago, I was able to have 2 broadcastreceivers, one declared in my manifest, and the other one, declared in my activity.
Like here : What is the best way to handle callback from IntentService
But, since I have changed for LocalBroadcastReceiver, and changed sendOrderedBroadcast with sendBroadcast method, only the one registered in activity is only receiving the broadcast. I have read that localbroadcastreceiver can not be registered in manifest.
So how to wake up a broadcastreceiver which is not registered in an activity ?
I found my own answer, so I give the response for those who can be interested in.
I have two BroadcastReceiver class, one used when application is wake up, with :
ActivityBroadcastReceiver myReceiver = new ActivityBroadcastReceiver();
#Override
protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("com.mybroadcast"));
super.onResume();
}
and
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
super.onPause();
}
and in my manifest file :
<receiver
android:name="com.broadcastreceiver.NotificationBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.mybroadcast" />
</intent-filter>
</receiver>
To send my broadcast :
if(!LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast))
context.sendBroadcast(broadcast);
I agree that with this, context.sendBroadcast() , everyone can received my broadcast, but in my code, sendBroadcast send only non-sensible data. That is the best solution I got for now.

Broadcast receiver works only when device reboot android

Hi I am developing android application in which I am defining one broadcast receiver.I am calling receiver from my activity. I am defining broadcast receiver like this :
public class MyScheduleReceiver extends BroadcastReceiver {
private static final long REPEAT_TIME = 100 * 5;
#Override
public void onReceive(Context context, Intent intent) {
Log.i("RRRRRRRRRRRRRRRRRRRRRRRR", "on receive");
}
}
In android manifest file I am defining like this:
<receiver android:name="abc.xyz.MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in main activity I am calling my broadcast receiver like this :
//in activity oncreate
startService(new Intent(this, MyScheduleReceiver.class));
My problem is that when call start service it's not starting my service actually. But when i restart my device it start my service because I gave intent filter "BOOT_COMPLETED". what I wanted to do actually when i call start service my service must be start,
Am I doing something wrong. How to solve this problem?
Actual what happens here is that you can staring a broadcast receiver while starting the activity and this broadcast receiver starts listening BOOT_COMPLEATED is happening or not. When this happens it comes to onreceive . If you need to start a process doing in background you can use a a Service insted of BroadcastReciever. BroadcastRecievers are used to listen for some events to happen.Go through this, it will help you
Services
BroadcastReceiver
You're either confused, or you aren't wording your question well. What you have in your manifest (and how Android works generally) is that when BOOT_COMPLETED occurs, it will call that BroadcastReceiver you defined. It will not automatically start an activity or service. If you want to do that, you need to call startService or startActivity in your onReceive function of the receiver.
You do not start BroadcastReceivers. You start services, which are long term background processes. You register BroadcastReceivers to be informed of special events (like BOOT_COMPLETED). When one of the events you registered for occurs, it will create an instance of that class and call its onReceive.
Hopefully that clears things up. If not, I suggest you reread some tutorials on services and broadcast receivers, you seem to have the two confused.
startService call would only start a Service. MyScheduleReceiver here is a braodcast receiver. To trigger broadcast receivers, you generally have to send broadcasts and not call the startService.
to start broadcasts you need to send broadcasts not startService()
add this instead of startService(new Intent(this, MyScheduleReceiver.class));
Intent intent = new Intent();
intent.setAction("pakagename.MyScheduleReceiver");
sendBroadcast(intent);
I hope it helps.

Android receivers being ignored when activity is not active

I have a background service which has a receiver for connectivity change which only seems to be received if the activity is active.
#Override
public void onCreate() {
mContext = this;
IntentFilter connectivityChangeFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(receiver, connectivityChangeFilter);
I've set it up in the manifest as follows:
<service
android:name="com.myservice.TimeService"
android:label="com.myservice.TimeService" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</service>
I have another receiver for boot completed which works ok, which is registered as a receiver in the manifest (unlike this one).
Is the intent filter not enough to run a broadcast? I would want the receiver to call a method on the service so it needs to be able to access methods of the service but I don't think receivers can bind to services.
-- Update
In a nutshell, I want to know if I can statically declare a receiver that interacts with a service. Dynamic declaration works only if the app is active.
Use android sticky intent
A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.
example code here:

Categories

Resources