NullPointerExceptoin when passing value from activity to broadcast receiver - java

I have an easy question.
I have declared text view in main activity, and created it from XML (findViewById). I would like to pass this value to a subclass of broadcast receiver. Following is my Broadcast constructor:
public Broadcast(TextView text_dBm) {
this.text_dBm = text_dBm;
}
In my main activity I create a new broadcast object and pass my textview value inside, like this:
new Broadcast(text_dBm);
But I'm still getting null pointer exception on my text_dBm. Is there anyway (besides static methods) to pass values between activites and broadcast receiver?
Oh and yes. My broadcast receiver is registered programmatically (in service), and its running perfectly.
Thank you for your time!
P.S: I already checked some threads here in SO, but i didn't find an answer:
How to pass value from an activity in an broadcast receiver?
Main activity class:
public class MainActivity extends Activity {
TextView text_dBm, text_time, text_rssi;
Intent startServiceFromActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_dBm = (TextView) findViewById(R.id.textView_dBm);
new Broadcast(text_dBm);
startServiceFromActivity = new Intent(this, WifiService.class);
startService(startServiceFromActivity);
}
}
Broadcast receiver class:
public class Broadcast extends BroadcastReceiver {
WifiInfo wifiInfo;
WifiManager wifiManager_service;
TextView text_dBm;
public Broadcast(WifiManager wifiManager_service) {
this.wifiManager_service = wifiManager_service;
}
public Broadcast(TextView text_dBm) {
this.text_dBm = text_dBm;
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d("RECEIVER", "Receiver running"); // LOG
text_dBm.setText("textview"); // nullpointerexception
}
}

You can't pass around views using Intents. To do what you want to do, you will need your broadcast receiver to be an inner class of your activity. The receiver should be registered when activity is started and unregistered when activity is stopped. Else you will leak memory. That means that you will only be able to receive your messages when actually on the activity screen itself.
If you need to be able to receive broadcasts outside the activity, you will need to:
register your receiver in the manifest for a given action (or in a service, but don't forget to unregister it)
start the activity and pass the message to show in the textview using an intent extra
when the activity starts, check if the intent contains anything to show in the textview and do the necessary
From your comment:
Create the receiver as an inner class of the activity (not a static one so it can access the activity's TextView instance)
register the receiver in onStart
unregister the receiver in onStop
In the onReceive method of your receiver do: textView.setText(intent.getStringExtra("dbm"));
Service sends the broadcast by passing an intent extra called "dbm" and containing the text you want to display
-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbmView = (TextView) findViewById(R.id.textView_dBm);
}
#Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter("com.example.broadcasts.DBM_UPDATE");
registerReceiver(receiver, intentFilter);
}
#Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
private TextView dbmView;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
dbmView.setText(intent.getStringExtra("dbm"));
}
}
}
In the service:
Intent i = new Intent("com.example.broadcasts.DBM_UPDATE");
i.putExtra("dbm", "it works!");
sendBroadcast(i);

Pass data Through intent
Activity -
Intent i = new Intent(Activity.this, Broadcast.class);
Bundle b = new Bundle();
b.putString("key", "value");
i.putExtras(b);
startActivity(i);
In your broadcast receiver class onReceive method
#Override
public void onReceive(Context context, Intent intent)
{
String result = intent.getString("key");
// your method
}

Related

Pass int to another Activity

Before you flame me:
I know there are uncountable tutorials out there, and I know myself how to pass data to another ACtivity, just like that.
In my case that's diffrent tho. "Usually" data is passed to another activity through Intents, Bundles ecc and the other Activity is started.
Here's my case:
I have an Item with 4 parameters (Image, String,String, int)
In an AdapterClass I have a PopUpView which retakes those 4 parameters.
What I'd like to achieve is the following:
With the click of a button, the 4th parameter, the int should be sent to the Main activity and inserted in a textView inside the MainActivity, without (here's the main diffrence between this and the other questions)launching the Main Activity.
How can this be done?
TIA.
use BroadcastReceiver to send that 4th int to MainActivity
in PopupView do these:
Intent intent = new Intent("SOMEACTION");
intent.putExtra("4th_int", value);
activity.sendBroadcast(intent);'
//In MainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter("SOMEACTION");
this.registerReceiver(mReceiver , filter);
}
#Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(this.mReceiver );
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == "SOMEACTION") {
// retrieve the 4th int value and update something in MainActivity
}
}
};

Keep changes made by BroadcastReceiver

I'm stuck at this point:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String number = bundle.getString("Time");
GameTime.setText("" +number + " hours");
}
};
In another Activity, when a Button is pressed, the MainActivity get's an int.
Whenever I open the Activity, I cannot see the GameTime TextView with the number variable in it.
I know that the OnReceive method works, beacause I had put a toast in it, and I could see the toast after sending the int from the other Activity.
How can I keep the changes made to the TextView while changing Activities?
Thank you.
One way:
Define an interface in your activity & Implement the interface inside your activity and pass its reference to the other class and call that reference whenever you need.
Example:
a) Create an interface
public interface MyBroadcastListener{
public void doSomething(String result);
}
b) Initialize BroadCastReceiver
public class TestNotifAlarm extends BroadcastReceiver {
private MyBroadcastListener listener;
#Override
public void onReceive(Context context, Intent intent) {
listener = (MyBroadcastListener)context;
listener.doSomething("Some Result");
}
}
c) Implement the interface in Activity
public YourActivity extends AppCompatActivity implements MyBroadcastListener{
// Your Activity code
public void updateTheTextView(String t) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(t);
}
#Override
public void doSomething(String result){
updateTheTextView(result); // Calling method from Interface
}
}
Another Way :
a) Put a Receiver inside your Activity class
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
textView.setText(intent.getStringExtra("extra"));
}
};
b) Register BroadCastReceiver
registerReceiver(broadcastReceiver , new IntentFilter("trigger_broadcust"));
c) Call sendBroadcast
Intent intent = new Intent("trigger_broadcust");
intent.putStringExtra("extra", "data");
sendBroadcast(intent);

How to check internet connectivity using broadcast receiver and Change the intent

I am working on an Android application which requires constant listener of Internet connectivity. I am using Broadcast listener and successfully applied it. But my code only shows the Toast message.
I want to stop the current activity and show a default XML file which says "No Internet Connection". and whenever it connect the Internet, previous activity resumes.
ExampleBradcastReceiver.java
public class ExampleBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false
);
if (noConnectivity) {
Toast.makeText(context, "Disconnected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(exampleBroadcastReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(exampleBroadcastReceiver);
}
}
In the place of Toast Message, I want to show a default XML file whenever disconnected and resume activity whenever connected.
You can move ExampleBroadcastReceiver to MainActivity as an inner class. And since in Java inner classes have access to their parent classes' methods and fields, you can in onReceive method consider showing/hiding the Internet disconnected view.
public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(exampleBroadcastReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(exampleBroadcastReceiver);
}
private void showInternetDisconnectedView(boolean disconnected){
// show or hide based on 'disconnected'
}
private class ExampleBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
showInternetDisconnectedView(noConnectivity);
}
}
}
}
You need to move Broadcast receiver code into Activity and on receiving internet connection events you can stop current in progress activity and make internet failure layout visible there only as it is part of Activity class. If it is required through out the Application, then create Base activity and handle this there to avoid duplicating code on every screen.

Refresh listview adapter on GSM notification receive

Say listview.java is a file that creates adapter and assigns it to a listview.
What I want is when I receive a GSM notification I want to refresh that listview adapter. So as soon as I receive notification my listview should contain new data.
I am using onMessageReceived(String from, Bundle data) this method of GcmListenerService.
Can I call some method of listview.java from class MyGcmListenerService extends GcmListenerService {} that will refresh the adapter and assign to listview?
If user is already on listview layout then I want it to get updated with new data.
Any Idea folks how to do it?
More clarity
in listview.java I create an adapter using data from shared prefs.
when I receive GSM notification I do some changes to shared prefs. Now I want My adapter should be recreated from with new shared prefs and should be assigned to list view.
Now folks?
Inside the onMessageReceived(String from, Bundle data) you can try doing listviewAdapter.notifyDataSetChanged(). From the docs of notifyDataSetChanged
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
You can use broadcast receiver in service on method onRecieve like this
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
And in activity you will will have to implement changes like this
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
//here refresh your listview
// adater.notifyDatasetChanged();
}
};
#Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
and on receive method refresh your adapter using adater.notifydatasetchanged
Use BroadcatReceiver for refresh listview while receiving Push Notification.
In your activity of ListView add below code,
ListView Activity
#Override
public void onCreate(Bundle savedInstanceState) {
-------
IntentFilter filter = new IntentFilter(1001);
LocalBroadcastManager.getInstance(this).registerReceiver(
handlePushNewMessage, filter);
}
private final BroadcastReceiver handlePushNewMessage = new BroadcastReceiver() {
#Override
public void onReceive(Context context, final Intent intent) {
// Update list here and refresh listview using adapter.notifyDataSetChanged();
}
};
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(handlePushNewMessage);
super.onDestroy();
}
In MyGcmListenerService.java add below code on onMessageReceived()
Intent intent1 = new Intent(1001).putExtra("MESSAGE", message);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

refresh activity from service after notification

I have a service
public class GcmIntentService extends IntentService{...}
that manages the notifications.
When a notification arrives if the user is out of the application, with a tap on the notification the main activity is resumed and updated
public class MainActivity extends Activity {
...
lv = (ListView) findViewById(R.id.lv);
adapter = new Adapter(this, item);
lv .setAdapter(adapter);
...
}
but how can I update the activity if the user is already on it?
You have to use BroadcastReciever for this task:http://developer.android.com/reference/android/content/BroadcastReceiver.html
in Activity:
public class MainActivity extends Activity {
public static final String NOTIFY_ACTIVITY_ACTION = "notify_activity";
private BroadcastReciver broadcastReciver;
#Override
protected void onStart() {
super.onStart();
broadcastReciver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction.equals(NOTIFY_ACTIVITY_ACTION ))
{
//to do smth
}
}
}
IntentFilter filter = new IntentFilter( NOTIFY_ACTIVITY_ACTION );
registerReceiver(broadcastReciver, filter);
}
#Override
protected void onStop()
{
unregisterReceiver(broadcastReciver);
}
}
In Service:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.NOTIFY_ACTIVITY_ACTION );
broadcastIntent.putExtra("addtional_param", 1);
broadcastIntent.putExtra("addtional_param2", 2); //etc
sendBroadcast(broadcastIntent);
UPDATE
BTW It's better use LocalBroadcastManager for send broadcast inside the app. It uses the same way as normal broadcast, but first you create LocalBroadcastManager:
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(MainActivity.this);
and in the onStart:
manager.registerReciever(broadcastReciver, filter);
and in the onStop:
manager.unregisterBroadcast(broadcastReciver);
and in the service:
manager.sendBroadcast(broadcastIntent);
if you are using the list and you would like to reload the activity just to refresh the list then, I would suggest you to use following methods on your adatper as soon as some message arrives. This would refresh the content in the list, and list will be updated automatically (without restarting of activity)
adapter.notifyDataSetChanged()
This method notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
It's not a good option to reload the activity, but if there is no other solution rather than recreating the activity i would suggest to refresh the view itself.
Hope this helps.

Categories