Can you help me resolve this issue, I have implemented a receiver(registered via XML) which listens for particular local broadcasts, and in turn starts a service for further processing, but somehow that receiver is not receiving any broadcasts.
Although another receiver, which is registered locally through code is able to receive the broadcast, can you help me fix this. Below is my code.
// Sending broadcast
Intent intent = new Intent(Constants.ACTION_PROFILE_UPDATED);
LocalBroadcastManager.getInstance(POC.getAppContext()).sendBroadcast(intent);
// Receiver
public class LocalReceiver extends BroadcastReceiver {
private final String TAG = LocalReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "received"); // its not received
if(intent.getAction() != null){
String action = intent.getAction();
Log.i(TAG, "action = " + action);
if(action.equals(Constants.ACTION_PROFILE_UPDATED)){
// IN manifest
<receiver
android:name=".LocalReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="local.action.profile.updated" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This damn code is not working, no where in developer guide it says local broadcast wont be received, through receiver registered through xml.
Please help,
Thanks.
I have implemented a receiver(registered via XML) which listens for particular local broadcasts
That is not possible. LocalBroadcastManager does not work with manifest-registered receivers, only with receivers registered via registerReceiver(), called on the LocalBroadcastManager instance itself.
Related
Hi I'm trying to catch the sms content and use in my app, so I made a BroadcastReceiver with Permission and Manifests But when the device receive sms, my code doesn't run which means that the BroadcastReceiver doesn't fire.
I also checked many Articles inside and outside here, there're some:
Android Sms Receiver Result to Main Activity
SMS receiver didn't work
Android SMS Receiver not working
Broadcast Receiver not working for SMS
Here is a part of my Manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application.
...
...
<receiver android:name="com.example.android.receiver.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And This is my Receiver
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "SMS Received!", Toast.LENGTH_LONG).show();
}
}
I also tried to register the receiver dynamically inside activity onCreate() but nothing changed
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(2147483647);
registerReceiver(new SmsReceiver(), intentFilter);
Does anyone know where is the problem? It should just Toast that a message is recieved so I can continue work but the receiver doesn't seem to even fire
You should read Automatic SMS Verification.
public abstract Task startSmsRetriever ()
Starts SmsRetriever, which waits for a matching SMS message until timeout (5 minutes). The matching SMS message will be sent via a Broadcast Intent with action
SmsRetriever.SMS_RETRIEVED_ACTION.
When you are ready to verify the user's phone number, get an instance of the SmsRetrieverClient object, call startSmsRetriever, and attach success and failure listeners to the SMS retrieval task:
SmsRetrieverClient mClient = SmsRetriever.getClient(this);
Task<Void> mTask = mClient.startSmsRetriever();
mTask.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override public void onSuccess(Void aVoid) {
Toast.makeText(YourActivity.this, "SMS Retriever starts", Toast.LENGTH_LONG).show();
}
});
mTask.addOnFailureListener(new OnFailureListener() {
#Override public void onFailure(#NonNull Exception e) {
Toast.makeText(YourActivity.this, "Error", Toast.LENGTH_LONG).show();
}
});
When a verification message is received on the user's device, Play services explicitly broadcasts to your app a SmsRetriever.SMS_RETRIEVED_ACTION Intent, which contains the text of the message. Use a BroadcastReceiver to receive this verification message.
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch(status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// by sending the code back to your server.
break;
case CommonStatusCodes.TIMEOUT:
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
break;
}
}
}
Register Your BroadcastReceiver with the intent filter com.google.android.gms.auth.api.phone.SMS_RETRIEVED in your app's AndroidManifest.xml file.
<receiver android:name=".YourBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
Finally, Register your BroadcastReceiver in your onCreate() section.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
getApplicationContext().registerReceiver(broadcastReceiverOBJ, intentFilter);
For Demo purpose you should read Automatic SMS Verification Android
I found it myself. Here is code that works! It must contain SMS_DELIVER_ACTION which this does. (many on github do not!)
Go into Settings->AppsNotifications->DefaultApps->MessagingApp and "SMS App" will showup for you to pick.
https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html
https://www.androidauthority.com/how-to-create-an-sms-app-part-2-724264/
https://github.com/treehousefrog/SMS-Project-Part-2
You should request runtime permission to receive SMS(Android 6.0 and higher).
https://developer.android.com/guide/topics/permissions/overview
First make another app your default SMS app.
Then: Google Hangout --> Settings(Disable merged conversations) --> SMS (Disable SMS)
Or,
This example for mainfest:
<receiver android:name=".SmsBroadcastReceiver"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_DELIVER_ACTION" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
i have an app and it's important for me to detect time changes. so far i have receiver with proper intent filters like this:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.ACTION_TIME_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
and this is my receiver :
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences.Editor spe =PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
spe.putLong("UFCount", 1));
spe.putLong("FCount", 1));
spe.commit();
Toast.makeText(context.getApplicationContext(), "Your job is being done!!", Toast.LENGTH_LONG).show();
}
}
this receiver only works when the app is open\in-the-background . how can i make this receiver to work even if the app is closed?
Since the application is closed, there is no UI (i.e. no Activity) to display the Toast. At this point, there should be an exception in Logcat. You might also add a logcat trace before displaying the Toast, like Log.d("MyReceiver", "in onCreate()"); and you should see it even when the app is closed.
Instead of displaying a Toast, you could for instance send a local notification, which is allowed because it does not require an Activity from your app.
I have an app in which I need to detect users Wifi connection and based on that, the user can see some data. The point is that I want to get notified when the user was on some other network while starting the app and moves to some other network while the app was running. Let me make a scenario:
Suppose I have 2 wifi, one inside my house and other outside. When starting the app, I was inside the house and accordingly the data shown to me was "ABC". Now when I move outside the house, my app should give a notification and kill the activity and hence I should not be able to see the same data "ABC" when I am outside the house.
Write a BroadcastReceiver as follows
public class TheBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
NetworkInfo datainfo = intent
.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (datainfo != null)
{
if (datainfo.getType() == ConnectivityManager.TYPE_WIFI)
{
//have different network states here
if (datainfo.getState() == datainfo.State.CONNECTING || datainfo.getState() == datainfo.State.CONNECTED) {
//work accordingly
}
}
}
}
}
Register a BroadcastReceiver and register these entries at manifest
<receiver android:name="yours package details like com.a.b.c.TheBroadcastReceiver " >
<intent-filter>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
Add following permission sets at manifest
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
Also visit http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
Each time the user changes the connection you can catch in BroadcastReceiver.
First of all you require to declare following permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Create a BroadcastReceiver
public class BrodcastNetwork extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Write your code here
}
}
Apply the filters to BroadcastReceiver
<receiver android:name="com.example.datausage.BrodcastNetwork" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
</action>
</intent-filter>
</receiver>
Register a broadcast receiver to listen for the Action "android.net.ConnectivityManager.CONNECTIVITY_ACTION"
When connection changed to "connected" state, call WifiManager.getConnectionInfo().getBSSID() to check the current access point ID
Hello
I want to check whether my service is running or not, if service is running, do nothing.
But if service is not running, restart the service.
So I do something like this.
In Manifest.xml
<receiver android:name="com.varma.android.aws.receiver.Receiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Receiver.java
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("aws", "Received...");
if(isMyServiceRunning(context)) {
Log.v("aws", "Yeah, it's running, no need to restart service");
}
else {
Log.v("aws", "Not running, restarting service");
Intent intent1 = new Intent(context, Service.class);
context.startService(intent1);
}
}
private boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (Service.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
But nothing is happening when I ON/OFF screen
What am I doing wrong?
You can't register screen on/off broadcast through manifest file,tt doesn't work(need to explore why).
Register it in your main activity through code
ifilter=new IntentFilter();
ifilter.addAction(Intent.ACTION_SCREEN_OFF);
ifilter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(new Receiver(), ifilter);
Till the time your activity remains in memory you will receive these broadcasts in your receiver, i have tested it and able to receive broadcasts.
However if your activity gets finished you won't receive these broadcasts. So registerting these broadcasts in some LocalService in your app with START_STICKY feature will solve your issue.
If you want your service to stay up, you don't need to do this. Just have onStartCommand return START_STICKY. This will cause Android to restart any service it stops as soon as it has sufficient memory.
I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
Although the Java constants are android.content.intent.ACTION_USER_PRESENT, android.content.intent.ACTION_BOOT_COMPLETED, and android.content.intent.ACTION_SCREEN_ON, the values of those constants are android.intent.action.USER_PRESENT, android.intent.action.BOOT_COMPLETED, and android.intent.action.SCREEN_ON. It is those values which need to appear in your manifest.
Note, however, that a receiver for ACTION_SCREEN_ON can not be declared in a manifest but must be registered by Java code, see for example this question.
Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
Add manifest tag
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
Create a receiver class and add your codes
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Create a service and register the receiver in it
public class MyService extends Service {
MyReceiver receiver = new MyReceiver();
#Override
public IBinder onBind(Intent intent) { return null; }
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(receiver);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Don't forget to define the service in manifest
<service android:name=".MyService"/>
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like alarm managers, jobs, etc which is not related to this question.
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in receiver tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:
<receiver android:name=".MyReciever">
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</receiver>
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck