FANDOM



If you need to only receive broadcasts when your Activity is at the Front, then this example from "50 Android Hacks", can be used to do that... (20.1)


public class MainActivity extends Activity {

private ProgressDialog mProgressDialog;
private TextView mTextView;

private BroadcastReceiver mReceiver;
private IntentFilter mIntentFilter;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);


mReceiver = new MyServiceReceiver();

mIntentFilter = new IntentFilter(MyService.ACTION);

startService(new Intent(this, MyService.class));

}


@Override
protected void onResume() {

  super.onResume();
  registerReceiver(mReceiver, mIntentFilter);

}

@Override
public void onPause() {

  super.onPause();
  unregisterReceiver(mReceiver);

}


private void update(String msg) {

//something here

}



class MyServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

  update(intent.getExtras().getString(MyService.MSG_KEY));

}

}

}


ewf