There are some question about Android4.2 Broadcast,
android.intent.action.TIME_TICK
android.intent.action.PACKAGE_INSTALL
This two protected broadcast are define in /frameworks/base/core/res/AndroidManifest.xml
Only find where register Receiver use Context.registerRecriver(),
Question:Where to send this Broadcast attached android.intent.action.TIME_TICK
Thanks
If you declare in manifest file it wont be enough.Manifest file is like a blue print.You need to declare the broadcast receiver there and after that u need to register the broadcast receiver in your code here i am attaching sample broadcast receiver code.
batteryLevelFilter= new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(batteryLevelReceiver, batteryLevelFilter);
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//yourcode
}
};
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().
And it only be sent by the system.
You have to register this intent programmatically : Sent every minute.
like this
IntentFilter if = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(YourReceiver, if);
Related
I was working with broadcast receiver and background services. So, When one my activity goes onPause(), I start my service and service sends a broadcast. Now, my broadcast receiver is in the same class as my service. I am receiving my service call, but I am unable to receive the broadcast info. Here is the code I have been working on..
[EDITED]
private String notifier = "ninja.ibtehaz.thenewproject.Activities.activityNew";
#Override
protected void onHandleIntent(Intent intent) {
boolean flag = ProjectApp.getInstance().isActivityVisible();
Log.e("rainbow", "onHandleIntent");
if (!flag) {
//start emergency activity
Log.e("rainbow", "starting activity");
Intent broadcastIntent = new Intent(notifier);
sendBroadcast(broadcastIntent);
}
}
[EDITED] on activityNew class >
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("rainbow", "In Method: Service started");
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("rainbow", "In Method: onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.e("rainbow","Screen went OFF");
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.e("rainbow","Screen went ON");
}
}
};
and the manifest file : [EDITED]
<service android:name=".Activities.ServiceBackground">
</service>
<receiver
android:name=".Activities.ActivityEmergency"
android:theme="#android:style/Theme.NoDisplay">
</receiver>
here is my logcat info :
E/rainbow: onHandleIntent
E/rainbow: starting activity
I am not getting anything after that..
I know this might not be the best practice, I just started working with these things.
Thank you for your help. Cheers!
You are registering a BroadcastReceiver with an IntentFilter which only contains Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON, but no ninja.ibtehaz.thenewproject.Activities.activityNew. It means this BroadcastReceiver can only be invoked when Intent.ACTION_SCREEN_OFF or Intent.ACTION_SCREEN_OFF is broadcast.
Now pay attention to your sendBroadcast code:
Intent broadcastIntent = new Intent(notifier);
sendBroadcast(broadcastIntent);
You are broadcasting an action "ninja.ibtehaz.thenewproject.Activities.activityNew", which can't match the IntentFilter bound with the BroadcastReceiver you have registered.
Try to use these codes:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(notifier);
registerReceiver(receiver, filter);
Broadcast Limitations : With limited exceptions, apps cannot use their manifest to register for implicit broadcasts. They can still register for these broadcasts at runtime, and they can use the manifest to register for explicit broadcasts targeted specifically at their app.
Android documentation: https://developer.android.com/about/versions/oreo/background
Try registering like below dynamically in Application Activity/Service instead of Manifest.
BroadcastReceiver receiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_UPDATE_NOW);
context.registerReceiver(receiver, filter);
You are registering your receiver inside the processing an intent method ;-) This would not work, definitely.
You must register your receiver earlier, ex. in onCreate() method.
Normally for a single IntentService you can define the broadcast receiver listener like this in an Activity's onCreate() method (for example)
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//get stuff from the intent and do whatever you want
}
};
And you register the receiver like this (also in onCreate()):
LocalBroadcastManager.getInstance(this)
.registerReceiver(broadcastReceiver, new IntentFilter("my_intent_service"));
And then you start the IntentService with:
Intent intent = new Intent(this, MyIntentService.class);
startService(intent);
And in the IntentService you send messages back to the receiver with:
Intent broadcastIntent = new Intent("my_intent_service");
broadcastIntent.putExtra("whateverData", whateverData);
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
which triggers the onReceive method described earlier.
And then to unregister the receivers, in onDestroy method you can do:
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
In the Manifest file, you add for your service:
<service
android:name=".MyIntentService"
android:exported="false" />
I want to receive broadcasts from multiple IntentServices in the same Activity.
Do you need one receiver per IntentService? As in, if I make n IntentServices do I need to register n receivers, make n listener, and unregister n receivers in onDestroy?
The way to filter multiple actions with one BroadcastReceiver is to add them to the IntentFilter:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("my_intent_service1"); // Action1 to filter
intentFilter.addAction("my_intent_service2"); // Action2 to filter
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
And in your BroadcastReceiver:
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("my_intent_service1")) {
// Action 1
} else if (intent.getAction().equals("my_intent_service2")) {
// Action 2
}
}
};
yes you can use one broadcast receiver in one activity, that would handle multiple intents from different services.
Iād recommend to add multiple intent-filter to your broadcast receiver to make distinction from which Service you are getting a broadcast.
I've learnt that BroadcastReceiver has to be declare in AndroidManifest.xml or registered in code like this:
BroadcastReceiver receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("some_action");
registerReceiver(receiver, filter);
But if I want to use a explicit Intent(which does not have IntentFilters) to start the BroadcastReceiver, how should I register it in code?
That is not possible. An explicit Intent will only work with a manifest-registered BroadcastReceiver.
i am new to Android and trying to understand the communication between apps.
I am trying to write 3 little apps which can communicate with each other. If you want to sent a message to everybody you just use an implicit broadcast.
implicit Intent intent.setAction("com.example.myChatMessage")
if you want to adress only 1 specifc receiver i did it with
explicite Intentintent.setComponent("com.example.test.android.broadcastreceiver.b",
"com.example.test.android.broadcastreceiver.b.myBroadcastReceiver")
this works, when the broadcast receiver is a seperate class and defined in the AndroidManifest.xml.
My Question: Is it possible to explicit adress a dynamicall registered broadcast receiver?
package com.example.test.android.broadcastreceiver.b;
public class MainActivity extends Activity {
private final IntentFilter intentfilter = new IntentFilter("com.example.myChatMessage");
private myBroadcastReceiver broadcastreceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastreceiver = new myBroadcastReceiver();
registerReceiver(broadcastreceiver, intentfilter);
}
public static class myBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("message", "B received: "+message);
}
}
}
It receives all implicit broadcasts but no explicit one - i don't know hot to adress it. Can you help me?
It does not appear possible to send an explicit intent to a dynamically registered broadcast receiver. Registering the receiver in AndroidManifest.xml is the only way.
If you dynamically register a BroadcastReceiver ā by calling Context.registerReceiver() ā you supply a BroadcastReceiver instance ... If you try to send an Intent to the receiver by naming the class of the BroadcastReceiver, it will never get delivered .. The Android system will not match the Intent you declared to the class of the BroadcastReceiver instance you registered.
Source: http://onemikro2nd.blogspot.com/2013/09/darker-corners-of-android.html
I have one Activity which creates a BroadcastReceiver with an IntentFilter in the method onCreate(...):
IntentFilter iFilter = new IntentFilter("action");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
registerReceiver(receiver, iFilter);
On the other side is an IntentService, which shall send some data:
Intent intent = new Intent(getApplicationContext(), receiver.class);
intent.setAction("action");
[...]
sendBroadcast(intent);
But it seems not to work. No Broadcast ist received.
My service class is in an android lib, perhaps this makes trouble.
Thanks for any advices.
Just create the intent with your action.
Intent intent = new Intent("action");
[...]
sendBroadcast(intent);
And consider renaming "action" to something more meaningful, like "com.my.package.actions.SOME_ACTION".
If you only want that your application components receive the broadcast then use:
Register a permission in your Manifest with a signature protection level (and define a use-permission for that permission). More here.
Use sendBroadcast(intent, permission), and specify the permission in 1.
if the intent is inside your app only, consider using LocalBroadcastManager