Reputation: 1893
I am developing an Android application and in that I am calling a webservice on one activity, every 5 seconds in background and now suppose their is 2 activities activity A activity B
on Activity A I am getting that web service response every 5 seconds and currently my control in on Activity B, now I want to show an alert from activity A by keeping control on activity on B.
In short I want to show some message which is coming in background on one page to the page where now I am.
I am new in android please suggest me some solution to this problem???
Thanks
Upvotes: 0
Views: 209
Reputation: 5216
Once solution that i would propose is this
Create a class called callAlertBox.java like this
public class callAlertBox {
private static Activity activity ;
public static Activity getActivity() {
return activity;
}
public static void setActivity(Activity mactivity) {
activity = mactivity;
}
public static void showMyAlertBox(){
AlertDialog ad = new AlertDialog.Builder(activity).create();
ad.setCancelable(true); // This blocks the 'BACK' button if false
ad.setMessage("Hello World");
ad.show();
}
}
And now in every activity that might call this alert box place this code on its onResume() method
@Override
protected void onResume() {
super.onResume();
callAlertBox.setActivity(Act1.this);
}
@Override
protected void onResume() {
super.onResume();
callAlertBox.setActivity(Act2.this);
}
Now you can call
callAlertBox.showMyAlertBox();
from any activity and you will get the alert box
EDIT
The MainActivity.java file
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent( MainActivity.this,Act1.class));
}
});
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent( MainActivity.this,Act2.class));
}
});
Button btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent( MainActivity.this,Act3.class));
}
});
new CountDownTimer(365 * 24 * 60 * 60, 5000) {
public void onTick(long millisUntilFinished) {
callAlertBox.showMyAlertBox();
}
public void onFinish() {
//Done
}
}.start();
}
@Override
protected void onResume() {
super.onResume();
callAlertBox.setActivity(MainActivity.this);
}
}
Now you can see from here my Countdown timer will call the method in the background casuing the dialog to appear independent of which ever activity you are in, In my case it was the main Activity and in you case there might be some updating method that is frequently called right ?? Just use that class to call the method
Upvotes: 0
Reputation: 3220
Basically you need publisher-subscriber pattern.
http://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
Check the link it is the simplest publishing subscribing class that I use, The concept is , Activity B subscribes to Activity A, So when Activity A has some property change then it fires the propertyChange which is received by Activity B, and future process is done by Activity B.
For subscribing to Activity A, Activity B implements PropertyChangeListener,
Upvotes: 1