I have registered a BroadcastReceiver in manifest:
<receiver android:name=".OrderReceiver" android:permission="com.google.android.c2dm.permission.SEND" android:exported="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.gndp" />
</intent-filter>
</receiver>
and in OrderReceiver class I'm trying to broadcast a received intent this way:
public class OrderReceiver extends BroadcastReceiver {
public void onReceive(Context mContext, Intent intent) {
if(intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
intent.setAction("com.gndp.device.REGISTERED");
mContext.sendBroadcast(intent); //BAZINGA
}
}
This broadcasted intent(BAZINGA) is received in this class(OrderReceiver) but not in another activity where i want to receive it. Here's the activity:
public class RegisterActivity extends Activity {
private BroadcastReceiver deviceRegisteredBroadcastReciever;
#Override
onCreate(){
...
deviceRegisteredBroadcastReciever = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//broadcast never reach here<----PROBLEM
}
}
#Override
onResume() {
...
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext);
lbm.registerReceiver(deviceRegisteredBroadcastReciever, new IntentFilter("com.gndp.device.REGISTERED"));
}
}
Have tried a lot of things including sending broadcast by instance of LocalBroadcastManager, using an inner class instead of BroadcastReceiver in the activity.
RegisterActivity is in foreground when OrderReceiver receives its first broadcast and it stays in foreground.
You send a global broadcast in the OrderReceiver by calling sendBroadcast of Context class and trying to receive this with LocalBroadcastManager.
You should either broadcast with LocalBroadcastManager or register ordinary BroadcastReceiver with Activity.registerReceiver method in the RegisterActivity.
Related
i register my (Broadcast Receiver) in service and when my main activity close my service destroid but my (Broadcast Receiver) Receiver working good?
and my question is how way to keep (Broadcast Receiver) in background?
and my solution is good idea ?
myservice code:
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Const.LOCATION_LOGGER_ACTION);
intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
registerReceiver(new BroadcastReceivers(), intentFilter);
return START_STICKY;
}
}
When you create BroadcastReceiver in code (Activity or Service), BroadcastReceiver should be managed by activity or service itself.
Otherwise, if you declare Broadcast Receiver in manifest, if your application is not running, system automatically create new process for your receiver. This is extremely useful when your application doesn't available such as BootCompleted).
<receiver android:name=".CustomReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
onReceive check service is running
The previous answer was correct. Add the code from there to Your manifest file,
then create new java class:
public class MyReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//Do what you need to do when broadcast is received
}
}
I have an application that use parse push notification service. Here is the class that I'm using for receiving notification:
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
}
And I also register this custom receiver in my manifest:
<receiver
android:name="com.package.MessageReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
Everything is fine with notification system. After a notification have received, I want to update the running activity UI accordingly (like showing an icon for new notification) but I don't have access to the activity object in onReceive method. What is the best practice to do that? I couldn't use the context object in this matter.
Thanks
I believe you can accomplish this by using a BroadcastReceiver.
You would define the receiver in the activities you want to have access to,
See snippet below.
public MyActivity extends Activity
{
//... code
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//... update ui here
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("suitablename");
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
//... code
}
Then in your custom receiver send the broadcast.
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
Intent intent = new Intent();
intent.setAction("suitablename");
context.sendBroadcast(intent);
}
}
Don't forget to update the manifest
<activity
android:name=".MyActivity" >
<intent-filter>
<action android:name="suitablename"></action>
</intent-filter>
</activity>
I'm starting with android and I'm having some lessons to learn some android concepts. In this case, I'm practicing with the BroadCast receivers.
I have to create a BroadCast Receiver that when I boot the phone/emulator, starts an activity which shows a plain text.
I have this class:
public class MainActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
But i'm not getting to do what I need, it simply does nothing, so... What I'm doing wrong here?
In the manifest I just have the activity declared.
Try this...
Step1:
set the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step2:
Add this is intent filter in receiver,
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Step3:
Now you can start your application's first activity from onReceive method of Receiver class..
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
You can not register ACTION_BOOT_COMPLETED receiver dynamically(it's not a way).
ACTION_BOOT_COMPLETED receiver has to be registered statically in manifest file
ex
<receiver android:name="com.myapp.receiver.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When you register a receiver in Activity or in Service it's lifetime is bound with them
You need to define your receiver inside app manifest.
For ex:
Class
class MyClass extends BroadCastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
}
Create your own MyReceiver class by extending BroadcastReceiver class, and register your MyReceiver in the manifest with filter ACTION_BOOT_COMPLETED.
<receiver android:name="com.test.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have a broadcast receiver defined in the manifest with the android.bluetooth.device.action.ACL_CONNECTED in the intent filter.
It's triggered fine when the app is in the stack, but after I stop it from android settings it won't trigger anymore. any suggestions?
Update for Menny:
<receiver android:name=".auto.AppLauncher">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
</intent-filter>
</receiver>
Did you program a BroadcastReceiver in your Code?
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
//something you want to do here
}
}
}
I'm developing two Android applications.
The first one calls broadcast receiver from activity.
The second one contains broadcast receiver that is called from first application.
I've manage to do broadcast call when it is in the same application with caller activity.
But when I take receiver to separate project it doesn't work. What should I change?
This is how I register receiver:
<receiver
android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:process=":deltaFO">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.myapp.intent.action.FILE_OPERATION" />
</intent-filter>
</receiver>
This is how I send intent
Intent intent = new Intent("com.myapp.intent.action.FILE_OPERATION");
intent.putExtra("operation", operation);
context.sendBroadcast(intent);
This is class that receives intent:
public class TestReceiver extends BroadcastReceiver{
public final String TAG="TestReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"EXTERNAL BROADCAST...");
}
}