I did a lot of research, but I didn't get through it, so that i don't know how to realize my App. The App consists of 2+ Activities, that contain content, that should be updated by a service in the background. So I dont know how to do the connection, some say i should do ipc, but others say thats too much of work, as long as service and activites run within the same process. I concerned to easily create methods like ActivityOne.RefreshData(Data data) and call those within the service, but i did not manage to get it work until now. I hope you have some suggestions to me and sorry for my bad english!
cheers
If you only need to provide data/updates to your own activities then IPC is most certainly not needed.
To achieve this, I would reverse the orientation you seem to be describing and rather than have the service calling methods on the activity, have it pushing messages to a Handler provided to it by the Activity when/if it starts.
See:
http://developer.android.com/reference/android/os/Handler.html
http://mobileorchard.com/android-app-developmentthreading-part-1-handlers/
Note that if what you need to send from the service to activites is always the same type of object, you can simplify your implementation of handleMessage() by using the Message.obj field to hold your type and not bother with Bundles or parcelling. As in:
Handler impl in activity where NotificationModel is the type that the service always sends:
private Handler mNotificationListener = new Handler(){
#Override
public void handleMessage(Message msg) {
handleIncomingNotification((NotificationModel)msg.obj);
}
};
The service side of posting msgs to this handler looks like:
public class NotificationRouter {
private Application mContext;
private SparseArray<Handler> mListeners = new SparseArray<Handler>();
public NotificationRouter (Application app){
this.mContext = app;
}
public void registerListener(Handler handler){
mListeners.put(handler.hashCode(), handler);
}
public void unRegisterListener(Handler handler){
mListeners.remove(handler.hashCode());
}
public void post(NotificationModel notice){
Message m = new Message();
m.obj = notice;
for (int i = 0; i < mListeners.size(); i++){
Handler h = mListeners.valueAt(i);
h.sendMessage(m);
}
}
}
Related
I have a rather general question.
Assuming I have a RecyclerView in some kind of a MainActivity. The content of this RecyclerView is being updated in multiple places in other activities.
For example there could be the possibility to make new entries by starting a new Activity and saving it there.In that case I would intuitively start that activity with startActivityForResult() and receive the changes in the onActivityResult() method in the MainActivity.
But lets say deeper inside the application, there is the possibility to delete all entries. Now how do we notify the MainActivity about this change in the dataset? I thought about setting a flag of some kind and clearing it after the content has been updated in the MainActivity. But somehow using global variables does not really follow the principle of proper encapsulation, does it?
Sorry for this vague question, but I find it quite hard to properly handle information flow in Android in a elegant manner, so here we are.
How about a local broadcast? You can find the idea of broadcast in this document. You need local broadcast and it is preferred if you want to pass data within your app only.
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
You can use Handler to pass the Message in Activity and then You have to update RecyclerView. Like,
1) In Activity.
public static Handler mHandler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
if(msg.what == 1223){
//update RecyclerView
}
return false;
}
});
2) pass message When you want to update RecyclerView
Message msg = new Message();
msg.what = 1223;
Activity1.mHandler.sendMessage(msg);
You can use EventBus to handle it.
Define a class for your event
public static class MyEvent {
int event;
/* define your fields */
}
And prepare your subscriber in main activity
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(MyEvent myEvent) {
switch(myEvent.event) {
/* Do what you need */
}
};
Now where you need to make change, call your subscriber like this:
MyEvent myEvent = new MyEvent();
myEvent.event = 1;
EventBus.getDefault().post(myEvent);
You can read more about EventBus in here
If you were using RxJava2, RxAndroid. Then you could try this.
Create a Bus:
public final class RxBus {
private static final BehaviorSubject<Object> behaviorSubject = BehaviorSubject.create();
public static BehaviorSubject<Object> getSubject() {
return behaviorSubject;
}
}
In your WaitingActivity where you want to receive data(where you want not to use onActivityResult in your case)
Disposable disposable = RxBus.getSubject().
subscribeWith(new DisposableObserver<Object>() {
#Override
public void onNext(Object o) {
if (o instanceof DataObject) {
//((DataObject) o).getValue();
}
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
}
});
});
In your activity where you want to send data
RxBus.getSubject().onNext(dataObject);
startActivity(new Intent(CurrentActivity.class, WaitingActivity.class));
Finally don't forget to dispose your disposable to avoid memory leaks in your WaitingActivity
#Override
protected void onDestroy() {
super.onDestroy();
disposable.dispose();
}
Your data should be separate from view, in model. If some other activity changes data ideally recycler view must be updated from there. So no matter which activity does what, when you refresh data on load or resume of your recycler view you will always get correct results.
AsFirebaseMessagingService does not use the Main Thread, I am just wondering as all my code in all of my activities or fragments run in UI thread(Main Thread). Now suppose my activity's onCreate method is executing and then I receive the push notification. Will these two blocks of code run in parallel, or will the push notification code wait in the queue until onCreate() method OR Activity's last life cycle method gets executed?
Edit- As you are saying code will run parallelly then suppose I have a variable in App.java
public class App extends Application {
int ctr = 100;
}
StatusActivity.java
public class StatusActivity extends BaseActivity {
public void onCreate() {
fun();
}
public void fun() {
int d = App.ctr - 1;//Step 1 Here d = 99
int m = App.ctr - 1; // Step 3 Here m = 98
}
}
FcmListener.java
public class FcmListener extends FirebaseMessagingService {
Override
public void onMessageReceived(RemoteMessage mssg) {
App.ctr = App.ctr - 1;//STEP 2 // Now App.ctr = 99
}
}
Now as you can see in the above code there will be problems if push notif code executes in parallel with fun(). I want push_notif and fun() to run serially, where order doesn't matter but not in parallel.
As already pointed out in a parallel answer, the overriden methods of FirebaseMessagingService run in a background thread, so you should use synchronization strategies in order to access/use mutable object from different thread.
But the question I want to answer is a bit different. Let's for a moment assume, that overriden methods run on a main thread. So is there a possibility, that the order of execution will be STEP 1 then STEP 2 and then STEP 3?
Android works with a technique called MessageQueue, basically there are Messages posted on that queue, on which Looper loops and "parses/executes" them.
Now if we assume, that you are currently located on STEP 1, it means, that there was a particular Message which is currently being executed (hypothetically, let's assume that action is - perform onCreate() of this activity).
Until this message is fully executed there cannot exist another Message which might get have a chance to be executed. So if we assume, that Firebase dispatches an event on background thread but the actual overriden method is being run on main thread, then this overriden method would have chance to be executed only after current Message (activity's onCreate()) has finished. In other words, there would be posted another Message on the MessageQueue, which would perform onMessageReceived() when the Looper will give chance for this message to be executed.
So, theoretically, there is no chance that the ordering would be STEP 1 -> STEP 2 -> STEP 3.
If STEP 1 is already executed, then it will continue with STEP 3 and the STEP 2 (at some point in future, because you can't know what other Messages are already posted on MessageQueue).
See this article for more details about MessageQueue and related classes.
How about it?
class Sample {
private String message = null;
private final Object lock = new Object();
public void newMessage(String x) {
synchronized (lock) {
message = x;
}
}
public String getMessage() {
synchronized (lock) {
String temp = message;
message = null;
return temp;
}
}
}
Here is my 2 cents. You say,
Suppose my activity's onCreate method is executing and then I receive the push notification. Will these two blocks of code run parallelly or will the push notification code wait in the queue until onCreate method OR Activity's last life cycle method gets executed?
From the official documentation of FirebaseMessagingService:
Extending this class is required to be able to handle downstream messages. It also provides functionality to automatically display notifications, and has methods that are invoked to give the status of upstream messages. Override base class methods to handle any events required by the application. Methods are invoked on a background thread.
So its possible both methods execute at the same time. If you want to do the operations on a shared variable in your Application class, you can do thread safe operations using synchronize. See How to synchronize or lock upon variables in Java?. That will make sure only one thread is making changes at a time on that variable. If a new thread comes in, it waits for the lock to get free and then makes the changes on that variable. However this doesn't guarantee the order. It just means that one thread operates on it at time and is in FIFO order.
I suggest you a different approach, because using those global variables can lead to unexpected behavior.
If your ctr var is related to your activity, then keep it inside. If you need it on other activities consider passing it via the Intent as an extra.
Use LocalBroadcastManager to inform your activity that you received the push message
public class FcmListener extends FirebaseMessagingService {
public static final String ACTION_MESSAGE_RECEIVED = "ACTION_MESSAGE_RECEIVED"
#Override
public void onMessageReceived(RemoteMessage mssg) {
Intent intent = new Intent(ACTION_MESSAGE_RECEIVED) // put extra vars as needed
boolean delivered = LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
// 'delivered' is true if there is at least someone listening to the broadcast, eg. your activity
// If your activity is not running, then 'delivered' is false so you can act accordingly
}
}
Then inside your activity
public class StatusActivity extends BaseActivity {
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (TextUtils.equals(FcmListener.ACTION_MESSAGE_RECEIVED, action)) {
// do stuff with 'ctr'
}
}
};
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(FcmListener.ACTION_MESSAGE_RECEIVED);
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
}
}
I need to pass a huge amount of data (raw bytes) between threads - listener thread and another thread that deals with the data manipulation. Whats the best and fastest way to do that?
As others have pointed out it really depends on the task you have. I would suggest using a Message. For example if one of your threads is a producer of said byte[] data and the other is the consumer you may have something of the sorts:
public class ProducerThread extends Thread{
byte[] data;
public void run(){
while (isRunning){
Looper.prepare();
data = someMethodToGetYourByteData();
Message dataToSend = Message.obtain();
Bundle bundle = new Bundle();
bundle.putByteArray("data", data);
dataToSend.setData(bundle);
if (ConsumerThread.mHandler!=null){
ConsumerThread.mHandler.sendMessage(dataToSend);
}
Looper.loop();
}
}
and your consumer thread:
public class ConsumerThread extends Thread{
public static Handler mHandler;
byte[] data;
public void run(){
while (running){
Looper.prepare();
mHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mHandler.obtainMessage();
data = msg.getData().getFloatArray("data");
doSomethingWithData(data);
};
}
Looper.loop();
}
Keep in mind though that this may not be the best pattern to use, as many elsewhere on SO point out that it is not the best idea to extend a Thread, but it generally depends on the use case.
Let us say the two threads are T1 and T2. Let us say we implement them using separate AsyncTasks A1 and A2. Now execute A2 from the onPostExecuteMethod of A1 passing necessary parameters to the execute method. This works fine because onPostExecute method runs in foreground and with respect to ui thread so that the A2 is executed only when A! has finished executing.
Since your talking about large amounts of data. The best way is to wrap your byte[] in an Object. You can do your own custom class but most people use ByteBuffer because it already has a good manipulation API and can be used with the Java NIO sub-system.
We are seeing an issue where one of our intent services is unexpectedly retrieving a null String extra for some of our users. We haven't been able to reproduce this, and we don't know if it is random or consistent on an affected user's device. There doesn't seem to be a correlation between affected users and device type or Android version.
I am extending IntentService and implementing the handleIntent method like this:
#Override
public void handleIntent(Intent intent) {
String action = intent.getAction();
if (action.Equals(ACTION_MARK_UNREAD)) {
String messageKey = intent.getStringExtra(EXTRA_MESSAGE_KEY);
// messageKey is null for some users
}
}
With fields:
public static final String ACTION_MARK_UNREAD = "com.myapp.action.MARK_UNREAD";
public static final String EXTRA_MESSAGE_KEY = "extraMessageKey";
In a fragment, we start this service in rapid succession 6 times:
for (int i = 0; i < 6; i++) {
Intent i = new Intent(MyIntentService.ACTION_MARK_UNREAD);
i = i.setClass(mContext, MyIntentService.class);
i.putExtra(MyIntentService.EXTRA_MESSAGE_KEY, i.toString());
mContext.startService(i);
}
Any ideas why the service would retrieve null for the messageKey extra?
We have other areas in the app that start this same service, and we can't identify which one it comes from when this situation happens. However, from looking at logs, it seems to be from this fragment that I mentioned. Logs show that the client timestamp when null happens is several seconds after the previous occurrence. This could be because the service queue is slow to move, or my assumption could be wrong.
My first guess is the compiler issue that many other commented about and I figure some other errors on your examples like Equals with upper E for example.
But considering your code as an example just, I tested a lot this approach and the first thing that I concludes is no matter if your calls are inside a Fragment or Activity due the Context of Fragment is the same of parent Activity.
Reading the IntentService documentation we can read that:
All requests are handled on a single worker thread -- they may take as
long as necessary (and will not block the application's main loop),
but only one request will be processed at a time.
Here: http://developer.android.com/reference/android/app/IntentService.html
So we can conclude that all your X requests will be processed enqueued one by one.
Another part at Android documentation about IntentService we can read that:
Because most started services don't need to handle multiple requests
simultaneously (which can actually be a dangerous multi-threading
scenario), it's probably best if you implement your service using the
IntentService class.
Here: http://developer.android.com/guide/components/services.html#ExtendingIntentService
So with that information you can think if IntentService has been the approach that you need. Right?
Here you can learn how extends just a Service: http://developer.android.com/guide/components/services.html#ExtendingService
Finishing, below I'm pasting the code that I made to test your approach.
Main Activity class MainActivity.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for (int i = 0; i < 10; i++) {
Intent intent = new Intent(MyIntentService.MY_ACTION);
intent.setClass(this, MyIntentService.class);
intent.putExtra(MyIntentService.EXTRA, UUID.randomUUID().toString());
startService(intent);
}
}
}
The IntentService MyIntentService.
public class MyIntentService extends IntentService {
public static final String MY_ACTION = "my.app.namespace.action.myaction";
public static final String EXTRA = "my_extra";
public MyIntentService() {
super("MyIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (action.equals(MY_ACTION)) {
String messageKey = intent.getStringExtra(EXTRA);
Log.i(EXTRA, messageKey);
}
}
}
And the output Log.
my_extra﹕ b6faeb0a-29fa-442b-b87e-9c7a5f8c35d7
my_extra﹕ 88076250-d455-4084-af5f-c560ba6d5570
my_extra﹕ 21339466-25ab-4aaa-aadd-344555c4c2df
my_extra﹕ 2f935a93-465b-4648-a3cc-60f0c9cc67a4
my_extra﹕ 128653d1-d6af-499f-8725-78158e2e7190
my_extra﹕ e453ae7b-e21a-41fe-bf9c-f45ccfd13edf
my_extra﹕ 2e3fc6aa-e425-41dd-a584-8ab056fb906d
my_extra﹕ a8d90d53-c6cd-4d15-84f9-4064d6972de9
my_extra﹕ 721dd17b-b977-4029-ada3-5999f0eb36e7
my_extra﹕ e83d3277-adc8-47a8-a246-6cd7f6f2735d
Will you try?
for (int y = 0; y < 6; y++) {
Intent i = new Intent(MyIntentService.ACTION_MARK_UNREAD);
i = i.setClass(mContext, MyIntentService.class);
i.putExtra(MyIntentService.EXTRA_MESSAGE_KEY, y.toString());
mContext.startService(i);
}
Could be a compiler issue...
I am trying to send the events from one Java class to Activity.
Scenario is, Will be having some data in the native, native will call the callback function which is in java code, This class processes data, after the processing i need to update the UI. I want to update the UI at one place in the Activity handler. (Dont want to use runOnUiThread() everywhere).
I was not able to send the events properly with the below approaches.
1st Approach:
1) Define functions for posting messages in to the queue and call these functions.
2) To call the above mentioned functions (point 1) we need context, if i maintain the static variable for maintaining the context and returning it, if the activity is created twice we wont able to get the write context for the first activity.
public class Activity1 {
protected static Context myContext = null;
protected Handler myHandler = null;
#override
public void onCreate() {
myContext = this;
myHandler = new Handler();
}
public static Context getMyContext() {
return myContext;
}
public void postEvent1() {
myHandler.sendMessage();
}
}
2nd Approach:
1) Making the handler as a static variable and returning this with the help of static function. - Not a good design to expose the internal variables.
2) Cons will be like above, when a second activity is created.
public class Activity1 {
protected static Handler myHandler = null;
#override
public void onCreate() {
myHandler = new Handler();
}
public static Context getMyHandler() {
return myHandler;
}
}
Is it possible to get the activity context without using the static variables and static functions?
Please share the knowledge if anyone knows. :)
Thanks & Regards,
SSuman185
I used a container class HashMap for storing the contexts with the key.
I used the name of the class as the key.
When the second activity is trying to register with class containing hashmap, it will reply with context of the already stored activity (null if not).
So like this I am able to store the contexts' of the classes and avoid loosing of the first activity context if I am creating the second one.
Please add if any one gets better solution.