I have an activity class A for the layout and a non-activity class B for Location Request; the class B triggers some events like onConnectionSuspended and i want to get these back in the classe A. How can i do that ?
Thank you for the previous answers, i worked on it but i add some details.
I want to separate the Activity code from the GoogleApi.Connection code. To do that i made a non-activity class (maybe a service is better ?) with Connection code (see the structure code below).
1) Is it a good idea ?
2) If it is not, what is better to do (maybe put all in the same Activity ?) ?
3) If it is yes, i don't think that i can put some "Toast" in the connections events of my non-activity code so how can i get back the connections events in the Activity to display some messages ?
Thank you for the answers
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class SATlocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
use Local broadcast receiver add receiver in your activity as below
private BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// intent can contain anydata
}
}
inside onresume register receiver as below
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(MyIntentService.ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, intentFilter);
}
unRegister receiver in onPause
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
}
In your location service add following code to send local broadcast
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);
As an alternative to broadcast receivers, if you have access to your class B instance from your Activity, you can also create an interface for listening to those events.
public interface LocationRequestEventListener {
void onConnectionSuspended();
...
}
public class SomeActivity extends Activity implements LocationRequestEventListener {
private ClassB classB;
@Override
public void onCreate() {
...
classB.setListener(this);
}
...
@Override
void onConnectionSuspended() {
//react to the event here
}
}
public class ClassB {
private LocationRequestListener listener;
....
public void setListener(LocationRequestListener listener) {
this.listener = listener;
}
private void connectionSuspended() {
// suppose this is where your class fire the event of interest
if (listener != null) {
listener.onConnectionSuspended();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.