How to Interface onReceive Method - android

I've set up a class called NetworkStatus that monitors the network status of the device. Within NetworkStatus I've defined a BroadcastReceiver that monitors if there has been a connectivity change, i.e., if the internet has been switched off or on. I want to 'expose' this method (i.e. onReceive) to the activity that instantiated the instance of NetworkStatus.
I'm trying to achieve this by setting up an interface in the NetworkStatus class, but I'm not sure how to call the interface from the onReceive method of the BroadcastReceiver i.e.,
public class NetworkStatus {
public static interface NetworkStatusCallbacks {
void onReceive();
}
public static class ConnectivityChange extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// need to call NetworkStatusCallbacks onReceive here
}
}
}
Then in the main activity I would do something like,
public class MyActivity extends Activity implements NetworkStatus.NetworkStatusCallbacks {
#Override
public void onReceive() {
// Do stuff here
}
}
Would really appreciate some pointers. Thanks.
Possible Solution
Think I have found a solution. As Selvin pointed out, to do this requires the Activity. I therefore obtained the Activity by casting it from the activity context that is passed to a constructor for the NetworkStatus class, i.e., setting a constructor for the class as
private static nsc NetworkStatusCallbacks;
public NetworkStatus (Context context) {
Activity activity = (Activity) context;
nsc = (NetworkStatusCallbacks) activity;
}
I can then call the interface from the onReceive method of the BroadcastReceiver as follows:
public void onReceive(Context context, Intent intent) {
nsc.onReceive();
}

Try something like this:
public class NetworkStatus {
public static interface NetworkStatusCallbacks {
void onReceive();
}
private final NetworkStatusCallbacks m_cb;
NetworkStatus(NetworkStatusCallbacks cb)
{
m_cb=cb;
}
public static class ConnectivityChange extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// need to call NetworkStatusCallbacks onReceive here
m_cb.onReceive()
}
}
}

Related

how to call non static method from static inner class within same activity

public class ABC extends Activity{
public void foo(int val)
{
}
...
public static class Receive extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
....
}
}
...
}
can anyone help me how can i call foo() from onReceive() of Receive class
public class YourActivity extends AppCompatActivity {
static YourActivity instance;
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
instance = this;
}
...
...
public void foo() {}
static class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
instance.foo();
}
}
if you cant make foo method static then you can remove the static from receive class.
public class Receive extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
}
}
Move public void foo(int val) to Class Receive

Access method and varaible in MainActivity from extern BroadcastReceiver

My MainActivity class is big. Therefor, I want to seperate the inner class BroadcastListener into normal class but I am facing the problem that I do not know how can I access the method and varaible in the MainActivity from this BroadcastReceiver class. Is there a way to do that?
I appreciate any help.
private class BroadcastReceiverListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
// This method"deliverBestAccessPoint" is in MainActivity class
String a = deliverBestAccessPoint(updatedResults);
//I want to set the "textwifi" varaible in MainActivity
textWifi.setText(a.toString());
}
}
else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
}
}
Create one interface as
interface ReceiverInteface
{
onBroadcastReceive();
}
let mainActivity implements this interface
mainactivity implements ReceiverInterface
{
#override
onBroadcastReceive()
{
//do all you task here
}
}
And BroadCastReceiverListner Class
pass refrence of interface from Mainactiivty.
ReceiverInteface recevierListner;
BroadcastReceiverListener(ReceiverInteface mListner)
{
recevierListner = mListner;
}
private class BroadcastReceiverListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
recevierListner.onBroadcastReceive();
}
}
}

How to make dynamically registered receive from outside of activity ?

I have a class structure as below
public class Admin extends DeviceAdminReceiver
{
public static class PubSub extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
messageIntentReceiver = new MQTTMessageReceiver();
IntentFilter intentCFilter = new IntentFilter(MQTTService.MQTT_MSG_RECEIVED_INTENT);
registerReceiver(messageIntentReceiver, intentCFilter);
}
#Override
protected void onDestroy()
{
unregisterReceiver(messageIntentReceiver);
super.onDestroy();
}
public class MQTTMessageReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle notificationData = intent.getExtras();
String newTopic = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_TOPIC);
String newData = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_MSG);
Log.e("Received Message on",newTopic+"- "+newData);
}
}
}
}
The receivers are dynamically registered and unregistered. The broadcast receiver works while PubSub activity stays on the screen. How to make it work from outside of activity?
I tried to do it in static way by registering in manifest.xml, but that dint work as MQTTMessageReceiver is a non static inner class. Cant able to instantiate the receiver runtime error I get.
I cant change the MQTTMessageReceiver class static as i need to access outer class members.

Calling a activity method from a BroadCast Receiver

I am looking around on how to call a method which is there in an AbstractActivity from a BroadCast Receiver. I tried making the method static and calling it straight away from the broadcast receiver just by using the classname. But the app then crashes with a nullpointer exception in the switch statement which is there in the static method.
Example:
public class MainActivity extends AbstractActivity{
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void additionDone(Context context,int number){
switch(number)
case 1:
Toast.makeText(context, "First case" , Toast.LENGTH_SHORT).show();
break;
}
}
BroadCast Receiver example:
public class receiver extends BroadcastReciever{
#Override
public void onReceive(Context context, Intent intent) {
//logic
}
public class anotherMethod(){
MainActivity.additionDone(1,context);
}
}
Is something wrong with the code above? Or is there a different way of calling a static method which belongs to a abstract activity?
The order of the parameters/arguments don't match.
additionDone(Context context,int number)
MainActivity.additionDone(1,context)
Your Context context; of the MainActivity is never assigned,
change your code to this
public static void additionDone(int number, Context context){
...//work with that context
}
and in your reciever
MainActivity.additionDone(1, context); //call with the context you got in your receiver constructor

The right way to dynamically control location tracking (registering / unregistering location broadcast receiver)

I want to dynamically control location tracking (registering / unregistering location broadcast receiver). This is how I am planning to do it. I have two questions :
What are the mistakes in the implementation below because all this concept is still very theoretical to me as I am very new to android/java dev. Still building concepts!
How do I pass some EXTRA_INFO from my location library class to the location receiver.
IMPLEMENTATION:
I have a library class LocationLibrary.java which consists of two methods. They do as the name suggest. The location tracking should start when I call startTracking(). Plz note the extraInfo that needs to be passed to myLocationReceiver. The tracking should stop when stopTracking() is called.
Code snippet:
public class LocationLibraray
{
private static BroadcastReceiver myLocationReceiver;
public LocationLibraray(Context context)
{
this.ctx = context;
myLocationReceiver = new MyLocationReceiver();
}
public void startTracking(Context context, String extraInfo)
{
IntentFilter filter = new IntentFilter();
filter.addAction("com.app.android.tracker.LOCATION_READY");
context.registerReceiver(myLocationReceiver, filter);
// NEED TO PASS extraInfo to myLocationReceiver for some processing, but HOW?
}
public void stopTracking(Context context)
{
context.unregisterReceiver(locationReceiver);
}
}
MyLocationReceiver.java
public class MyLocationReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
//GET THAT EXTRA INFO FROM LocationLibrary class and process it here
}
}
}
Please help me out. Thnx!
Why not add a constructor to MyLocationReceiver?
public class MyLocationReceiver extends BroadcastReceiver {
String info = "";
public MyLocationReceiver(String extraInfo)
{
this.info = extraInfo;
}
........
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
if (info.contains("Hi"))
//do some stuff
}
}
}
And you would instantiate it like this:
myLocationReceiver = new MyLocationReceiver(new String("Hello!"));

Categories

Resources