How to communicate between android and another process using broadcast receiver
but it doesn't work
That is because you over-specified your Intent
. Replace:
Intent locationIntent = new Intent("LocationIntent");
locationIntent.setAction("updatedLocations");
locationIntent.setClass(getApplicationContext(), MapBoxActivity.class);
locationIntent.putExtra("list",updatedList);
sendBroadcast(locationIntent);
with:
Intent locationIntent = new Intent("updatedLocations");
locationIntent.putExtra("list",updatedList);
sendBroadcast(locationIntent);
Note, though, that any app will be able to listen to this broadcast. Consider using setPackage()
on the Intent
to restrict the delivery to your own app.
What's the best way to communicate between Activity and Service defined in another process and getting data continuously?
If I were forced into this process separation, I would consider a Messenger
.
my service gets data from server but the server is not realTime so I check the data by requesting periodically to server using handlers.
That hasn't been a recommended pattern in years. Please use WorkManager
. Or, use JobScheduler
, if you are not in position to adopt WorkManager
(since it is part of AndroidX). In either of those approaches, you could get rid of the second process and greatly simplify your communications.
android communicate between activity and broadcast receiver
I think building the receiver as a private subclass of your activity is the way to go here. This way you can control events and data from your activity. Then you can just create one instance of it and register the receiver from code as you did above.
Note that you don't have to register your receiver in both the manifest and code. One of these is enugh - the manifest is basically a "static" registration while doing it in code allows dynamic registration at runtime. Also when you register in the manifest, a new instance of your receiver will automatically be created from the system, executed and terminated. Doing the reg in code allows to point to one specific instance.
Android best way to have a broadcast receiver communicate with a service and an activity communicate with the service
There are many ways to do this and there is no "best way". It all depends on what other communication is going on between these components.
Activity
can send data to Service
using extras in an Intent
on startService()
.
Service
can send data to an Activity
using extras in an Intent
on startActivity()
Service
can send data to any component using extras in a broadcast Intent
. Other components (activities, broadcast receivers, etc.) can register BroadcastReceiver
s to listen for the data from the Service
.
Activity
can also bind to a Service
which will allow 2 way communication between the Activity
and Service
using method calls (AIDL). These method calls can be synchronous or asynchronous.
If the components are all running in the same process, you can store state in static
variables and share data that way.
You need to look at your whole application and see what data needs to move from component to component and then choose a method that satisfies those requirements.
communication between activity and broadcast receiver
No, you are trying to instantiate an Activity
via its constructor. Never do that - Activities have special initialization steps that allow them to become proper Activities (and as a result Contexts).
The Context
class (which your BroadcastReceiver
receives an instance of via the onReceive()
method) is the the class that contains methods such as getSharedPreferences()
.
Use that instead.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent){
String s = context.getSharedPreferences("Shared_Prefs_Name",Context.MODE_PRIVATE).getString("Key","def_value");
}
}
Calling a Activity method from BroadcastReceiver in Android
try this code :
your broadcastreceiver class for internet lost class :
public class InternetLostReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.sendBroadcast(new Intent("INTERNET_LOST"));
}
}
in your activity add this for calling broadcast:
public class TestActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(broadcastReceiver, new IntentFilter("INTERNET_LOST"));
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// internet lost alert dialog method call from here...
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
}
Related Topics
Change Device Language via Adb
How Can Android Source Code Not Have a Main Method and Still Run
How to Rotate Textview 90 Degrees and Display
Android:Load Svg File from Web and Show It on Image View
Google Drive API Doesn't Play Well with Proguard (Npe)
Android Webview VS Phone Browser
Method to Get All Edittexts in a View
Application Content Goes Behind the Navigation Bar in Android L
Android Sdk Content Loader Failing with Nullpointerexception
Chaining Requests in Retrofit + Rxjava
Android Systemui Glitches in Lollipop
Difference Between Android: and App: Prefix in Android Xml
Load Images from Disk Cache with Picasso If Offline
Fused Location Provider Unexpected Behavior
Android: How to Create Fade-In/Fade-Out Sound Effects for Any Music File That My App Plays