refresh activity from service after notification - java

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.

Related

Passing data from Activity to Fragment using AsyncTask - Android

I'm trying to pass an ArrayList from an AsyncTask in the MainActivity to a fragment, but I'm getting a NullPointerException for invoking
CategoryAdapter.getItemCount() even if I'm passing the array after the BroadCastReceiver Invoke.
What Am I doing wrong?
MainActivity
class GetBooksAsync extends AsyncTask<Void, Void, Void> {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getApplicationContext());
#Override
protected Void doInBackground(Void... voids) {
for (ECategories category : ECategories.values()) {
try {
categories.add(new Category(category.toString(), apiClient.getBooks(category)));
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent("com.android.mainapp");
intent.putExtra("categories", categories);
manager.sendBroadcast(intent);
replaceFragment(new HomeFragment());
}
}
HomeFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initBroadCastReceiver();
categoryAdapter = new CategoryAdapter(categories,getContext());
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = view.findViewById(R.id.parent_rv);
recyclerView.setAdapter(categoryAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
categoryAdapter.notifyDataSetChanged();
return view;
}
private void initBroadCastReceiver() {
manager = LocalBroadcastManager.getInstance(getContext());
MyBroadCastReceiver receiver = new MyBroadCastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.mainapp");
manager.registerReceiver(receiver,filter);
}
class MyBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//get the categories from the intent
categories = new ArrayList<Category>();
categories = (ArrayList<Category>) intent.getSerializableExtra("categories");
}
}
i've also tried attaching the recyclerView from the OnReceive Method, but it's not getting attached.
Thank you in advance!
I think there are several problems with your code:
Your task is running in a different thread than the UIThread (which schedules the task and processes the result). That means it most probably runs on a different processor/core. Processed values (such as your collection) are cached in a processor and somewhen after execution the data is written back to RAM. But that might happen after the onPostExecute method is called, which takes the collection to another processor cache as well. But when this is done before the collection is returned to the RAM from the task, it's still empty. That's called a race condition.
Now there are several ways to solve that. The simplest one is to use Collections.synchronizedList(categories)
This prevents the processor from caching list values and always return it to the RAM (or using L3 cache which is shared between all processors/cores).
I'm not sure what exactly you pass to the collection. Intents (and it's data) need to be serializable and what you add to your collection is probably not serializable.
Then I would use the AsyncTask parameters:
class GetBooksAsync extends AsyncTask<ECategories, Void, Collection<Category>> {
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getApplicationContext());
#Override
protected Void doInBackground(ECategories... eCategories) {
Collection<Category> categories = [whatever you want to use];
for (ECategories category : eCategories) {
try {
categories.add(new Category(category.toString(), apiClient.getBooks(category)));
} catch (IOException e) {
e.printStackTrace();
}
}
return categories;
}
#Override
protected void onPostExecute(Collection<Category> categories) {
super.onPostExecute(categories);
Intent intent = new Intent("com.android.mainapp");
intent.putExtra("categories", categories);
manager.sendBroadcast(intent);
replaceFragment(new HomeFragment());
}
}
And note that AsyncTask and LocalBroadcastManager are deprecated.
Is Category serialized?
You can use BroadcastReceiver as an internal class, and then update the data of Adpater when it receives the data, because the code runs very fast, and it is not necessary to register for monitoring, and it will be processed immediately.
I guess the way you pass the data from MainActivity to HomeFragment is incorrect.
WHAT YOU EXPECT
Call MainActivity#GetBooksAsync
Wait till onPostExecute has been called
HomeFragment is ready to receive the broadcast message, then update UI
Broadcast the message from MainActivity to the fragment
WHAT IS HAPPENING HERE
Call MainActivity#GetBooksAsync
Wait till onPostExecute has been called
Broadcast the message from MainActivity. There is no receiver to receive this message!
HomeFragment is ready to receive the broadcast message, then update UI
HOW SHALL YOU PASS THE DATA THEN?
There are several way.
Broadcast data between the UI component like the things you did. But you will need to beaware the life cycle of the components. That is, when you broadcast the data, the receiver must already init and the UI component is in active.
Build a singleton class to store the data. Your activity and fragment treats the singleton class as a common place for the data storage.
Use Intent and the extra property to pass the data IF the data size is small enough.
Use LiveData. I believe it is the most modern way recommended by the community. Though I am not sure how its work.
To verify the fact that it is an life cycle issue,
you can try to add a delay before you sending the broadcast message.
class GetBooksAsync extends AsyncTask<Void, Void, Void> {
...
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent("com.android.mainapp");
intent.putExtra("categories", categories);
TimerTask task = new TimerTask() {
#Override
public void run() {
manager.sendBroadcast(intent);
}
};
Timer timer = new Timer();
timer.schedule(task, 5 * 1000); // Delay the broadcast after 5 seconds
replaceFragment(new HomeFragment());
}
Your Adapter should be written like this.
class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.VHolder>{
private ArrayList<Category> list = new ArrayList<Category>();
public void setList(ArrayList<Category> list) {
this.list = list;
notifyDataSetChanged();
}
public CategoryAdapter(Context context) {
// Do not pass a list in the constructor, because the list may be empty
}
class VHolder extends RecyclerView.ViewHolder {
public VHolder(#NonNull View itemView) {
super(itemView);
}
}
......
}
Your fragment should have a global Adapter for BroadcastReceiver to update data
public class Test extends Fragment {
// Create a global Adapter for BroadcastReceiver to call and update data
private CategoryAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
adapter = new CategoryAdapter(getContext());
initBroadCastReceiver();
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = view.findViewById(R.id.parent_rv);
recyclerView.setAdapter(categoryAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
private void initBroadCastReceiver() {
manager = LocalBroadcastManager.getInstance(getContext());
MyBroadCastReceiver receiver = new MyBroadCastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.mainapp");
manager.registerReceiver(receiver,filter);
}
class MyBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//get the categories from the intent
ArrayList<Category> categories = (ArrayList<Category>) intent.getSerializableExtra("categories");
adapter.setList(categories);
}
}
}

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
}
}
};

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);

Android - LocalBroadcast

I searched a lot but didn't find the solution to the following issue.
Context :
I use an AsyncHttpResponseHandler object to handle all my webservice
responses
If I get a particular error code from the WS, I want to show an alert dialog (whatever the activity currently displayed)
I came to think that using the LocalBroadcastManager would be a good solution as the HTTP handler is not aware of the currently displayed activity
Problem :
After implementation of all what seems to be needed to make it work, my intent sent from the async handler is not received.
Additional note :
the ApplicationContext is stored in my StaticItems
class which contains all the static data I need in my app.It is setup via a custom class which inherits from Application
If I broadcast the intent from an activity, the OnReceive event is triggered
I thank you in advance for any help you could provide.
Cheers !
Here are some pieces of my code :
The code in the http handler
public class AsyncResponseHandler extends AsyncHttpResponseHandler {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
if(response != null && response.length > 0) {
CrvData data = JsonHelper.getCrvData(new String(response));
String code = data.getErrorCode();
if (!TextUtils.isEmpty(code) && code.equals(StaticItems.C_WS_OBSOLETE_VERSION_ERROR)) {
Intent intent = new Intent();
intent.setAction(StaticItems.BROADCAST_INTENT_MESSAGE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(data.getValue()));
LocalBroadcastManager.getInstance(StaticItems.applicationContext).sendBroadcast(intent);
}
}
}
The code of the root Activity I use for all my activities :
public class MainActivity extends Activity {
/**
* The local broadcast receiver
*/
protected MyBroadcastReceiver mMessageReceiver = new MyBroadcastReceiver();
#Override
protected void onResume() {
super.onResume();
// register
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addAction(StaticItems.BROADCAST_INTENT_MESSAGE);
LocalBroadcastManager.getInstance(StaticItems.applicationContext).registerReceiver(mMessageReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
// Unregister
LocalBroadcastManager.getInstance(StaticItems.applicationContext).unregisterReceiver(mMessageReceiver);
}
}
The receiver
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setInverseBackgroundForced(true)
.setNegativeButton(context.getResources().getString(R.string.dlg_no), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(R.drawable.ic_dialog_alert_holo_light)
.setMessage(R.string.dlg_app_is_obsolete)
.setPositiveButton(context.getResources().getString(R.string.dlg_yes), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(intent);
}
});
builder.show();
}
}
I had a similar problem, for some reason you can not setData(..) on Intent returned via LocalBroadcastManager, when I placed my URI in the extra data, it started to work.... possibly a bug?
My guess is that the context you are using is not an activity context and thus can't be used for creating dialogs.
Namely, the context that is passed to onReceive in this case should be your application context, StaticItems.applicationContext, which is not good for showing dialogs.
If this is the case, you should somehow rearrange your code to make sure to pass an activity context rather than the application one.
Remove addCategory() from your IntentFilter
was solved here
OK so... I figured out that the intent was not received because of this line :
intent.setData(Uri.parse(data.getValue()));
And as #mvai pointed, the dialog cannot be displayed from the application context. I managed to get the current activity by having a reference to it in my application class. (I checked out to be sure for not having any memory leak)

NullPointerExceptoin when passing value from activity to broadcast receiver

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
}

Categories