I'm writing an Android app and need to recognize various system statuses, such as connection, battery status, and a few others,
My question would be, how to create a function for this?
I created the following code block
IntentFilter iFilter_Bateria = new IntentFilter();
iFilter_Bateria.addAction(Intent.ACTION_BATTERY_CHANGED); // pega a ação da bateria
registerReceiver(mBroadcast_Bateria, iFilter_Bateria); // registra o BroadCast no Android
IntentFilter iFilter_Status_Conexao = new IntentFilter();
registerReceiver(mBroadcast_Status_Conexao, iFilter_Status_Conexao); // registra o BroadCast no Android
IntentFilter iFilter_Status_Tela = new IntentFilter();
iFilter_Status_Tela.addAction(Intent.ACTION_SCREEN_ON); // pega a ação de ligar a tela
iFilter_Status_Tela.addAction(Intent.ACTION_SCREEN_OFF); // pega a ação de desligar a tela
registerReceiver(mBroadcast_Status_Tela, iFilter_Status_Tela); // registra o BroadCast no Android
IntentFilter iFilter_Usuario_Presente = new IntentFilter();
iFilter_Usuario_Presente.addAction(Intent.ACTION_USER_PRESENT); // pega a ação de usuario presente no aplicativo
registerReceiver(mBroadcast_Usuario_Presente, iFilter_Usuario_Presente);
But only the first broadcast and registered, I tested each function separately, and all worked fine, but as I said, always the first one is registered, the other not, how do I solve this?
private BroadcastReceiver mBroadcast_Bateria = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("B. Bateria", "Registrado");
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
if (isCharging) {
mTextCarregando.setText("Carregando");
} else {
mTextCarregando.setText("Não Carregando");
}
//////////////////////////////////////////////////////////////////////
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (usbCharge) {
mTextTipo.setText("Carregando Via USB");
} else if (acCharge) {
mTextTipo.setText("Carregando Via Tomada");
} else {
mTextTipo.setText("Não Carregando");
}
///////////////////////////////////////////////////////////////////////
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
mTextViewInfo.setText("Battery Scale : " + scale);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);
float percentage = level / (float) scale;
mProgressStatus = (int) ((percentage) * 100);
mTextViewPercentage.setText("" + mProgressStatus + "%");
mTextViewInfo.setText(mTextViewInfo.getText() + "\nPercentage : " + mProgressStatus + "%");
mProgressBar.setProgress(mProgressStatus);
}
};
private BroadcastReceiver mBroadcast_Status_Conexao = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("B. Conexão", "Registrado");
boolean wifi = Status_Conexao(context);
if (wifi) {
mTextWifi.setText("Conexão ok");
} else {
mTextWifi.setText("Sem Conexão");
}
////////////////////////////////////////////////////////////////////////////////
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//For 3G check
boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
//For WiFi Check
boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if (isWifi) {
mTextWifiTipo.setText("Via Wifi");
} else if (is3g) {
mTextWifiTipo.setText("Via 3G");
} else {
mTextWifiTipo.setText("Sem Conexão");
}
}
private boolean Status_Conexao(Context context) {
boolean connect;
ConnectivityManager connMgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
connect = true;
} else {
connect = false;
}
return connect;
}
};
private BroadcastReceiver mBroadcast_Status_Tela = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("B. Tela", "Registrado");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
mTextTelaLigada.setText("Tela Desligada");
chronometer.stop();
chronometer.setText("00:00");
TelaLigada = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
mTextTelaLigada.setText("A tela esta em uso");
chronometer.start();
TelaLigada = true;
}
}
};
private BroadcastReceiver mBroadcast_Usuario_Presente = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("B. Usuario", "Registrado");
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
UsuarioPresente = true;
mUsuarioPresent.setText("Usuario Presente");
} else
mUsuarioPresent.setText("Não Presente");
}
};