This should be fairly easy but I somehow can't get a Broadcast receiver's onReceive method triggered. Details below:
App B provides a broadcast receiver.
Manifest:
<receiver android:name=".MyNotificationReceiver">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Java:
public class MyNotificationReceiver extends BroadcastReceiver {
private final String TAG= "MyNotificationReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
}
}
App A is Broadcast Sender App:
Java
Intent intent = new Intent();
intent.setAction("com.go.foo.A_ACTION");
sendBroadcast(intent);
Log.d(TAG, "broadcast intent sent...");
I can see the log statement that the broadcast is sent but the receiver's onReceive() callback is not getting triggered. Am I doing something wrong?
Interesting why it's not working. Try this one out. I know the default values of exported and enabled are true. But still have a try at it.
<receiver android:name=".MyNotificationReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Learned something new today. As of Android 4.1, the system no longer sends broadcasts to app components without an activity. The broadcast received started working after I added an activity in the manifest.
Related
I am experimenting with broadcast receiver in android and I have this class as broadcast receiver:
public class BroadcastInbuilt extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Battery Low, Please Charge!", Toast.LENGTH_LONG).show();
Log.i("BroadcastPersonal","Happening");
MainActivity.b1.setText("Change text if working!!");
}
}
I have added this in Android Manifest inside application tag:
<receiver
android:name=".BroadcastInbuilt"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
There is no code in my main activity. When I change my battery level to low the toast does not appear. i am following the android tutorial and I have done exactly same as told here
https://www.youtube.com/watch?v=Uf4V5OSJji8&list=PLlyCyjh2pUe9wv-hU4my-Nen_SvXIzxGB&index=44
Result is at 9:40 in video. Now his app shows toast but mine does not.
I did some research and found out that you should not register your receiver in Android Manifest but register them dynamically through programming.
BroadcastReceiver myBroadcast = new BroadcastInbuilt(); // name of class which extends BroadcastReceiver
IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_LOW);
this.registerReceiver(myBroadcast, filter);
Be sure to unregister broadcast in onDestroy()
unregisterReceiver(myBroadcast);
Note: if receiver was registered in onCreate() then it should be unregistered in onDestroy().If it was registered in onResume() then it should be unregistered in onPause().
I am trying to get the Android BroadcastReceiver to run when a Firebase Cloud message notification is received by the Android system.
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "EVENT OCCURED", Toast.LENGTH_LONG).show();
}
}
It is required in the AndroidManifest to specify receiver tags as such:
<receiver
android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</receiver>
As you can see in the Manifest above I've added:
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
to make sure the BroadcastReceiver fires when I plug cable to Android device. It works fine.
Therefore the issue lies with the:
<action android:name="com.google.firebase.MESSAGING_EVENT" />
Is this intent-filter action not recognized by the BroadcastReceiver?
Is there another intent-filter action for Firebase messaging the BroadcastReceiver uses?
The Android Intent messaging system processes Intents in three "channels": Activities, Services and BroadcastReceivers. The methods for publishing an Intent indicate the channel type: startActivity(), startService() and sendBroadcast(). An intent published with startService() will only match the intent filter of a Service. It is not tested against Activities and BroadcastReceivers.
Because action com.google.firebase.MESSAGING_EVENT is normally received by a Service derived from FirebaseMessagingService, it must be the case that the action is published in the Service channel. You will not be able to receive it with a BroadcastReceiver.
I want to Start a service when phone reboot completed. I will install my app but will not launch it.
I have proceed with the following ways using Broadcast Receiver listening to BOOT_COMPLETED intent.
My Receiver is as follows...
public class MyReceiver extends BroadcastReceiver {
static final String TAG = "MyReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "******* OnReceive");
Log.d(TAG, "Boot Completed Caught");
context.startService(new Intent(context, MyService.class));
}
}
In Manifest I have added the following lines of code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name="MyService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.logic.MyService" />
</intent-filter>
</service>
<receiver android:name="MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But receiver is not listening once boot is completed. Remember I have only installed my app but never launch it.But my requirement is such that I should not launch my app after installing it in my device. Anybody can help me on this.
I googled it and found that as I have not launched my app it is in stopped state and boot completed intent is ignoring my receiver. The below links that might help you in understanding my issue:
http://devmaze.wordpress.com/2011/12/05/activating-applications/
the service and MyReceiver must be full packagename,
like this
I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));
I have an Android service which sends broadcast intents. I'm trying to get those intents in another application, which is an Android service. I wrote this in my manifest:
<!-- Service -->
<service android:enabled="true" android:name="...MyService"></service>
<!-- Receiver -->
<receiver android:name="...MyReceiver">
<intent-filter>
<action android:name="..."></action>
<action android:name="..."></action>
</intent-filter>
</receiver>
and this in my MyReceiver class:
public class ScannerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Process action.
Log.d(Globals.LOG_TAG, "Intent received.");
...
Unfortunately I never get the onReceive method invoked. Any idea why?
I start this service from another test application, so this is set as an Android library. The service is correctly started but this receiver is receiving nothing. Any idea what I'm doing wrong?
Thanks!
In manifest must be: android:name=".[package].ScannerBroadcastReceiver"
I solved the problem and I suppose it is due to the fact that this project was set as a library. If I don't set it this way the intent is correctly received. I haven't read about this anywhere.