I have the following situation: One main application is the launcher application (A), always the application is running, and this application call another a child application (B) fig 1 . The problem occurs when the application (A) launch an intent service
and the application(B) use this intent (fig 2).
This is the code used to launch the intent service:
Service
intentCodeRead.putExtra(BARCODE_TEXT, readStr);
intentCodeRead.putExtra(BARCODE_DATE, System.currentTimeMillis());
intentCodeRead.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intentCodeRead);
Any suggestion of flags to prevent this behaviour?
EDIT 1
In the application (A)( The launcher application) I have an intentService
that made a broadcast
to another applications. The application (B) is an application that listen to this broadcast. When (A) make a broadcast the application (B) listen then. The problem is that (A) bring to front of (B) when (A) do this broadcast. How can I made a broadcast
without change the order of applications presentation?
EDIT 2
application A
BarcodeScannerService.java
intentCodeRead.putExtra(Constants.BARCODE_TEXT, readStr);
intentCodeRead.putExtra(Constants.BARCODE_DATE, System.currentTimeMillis());
intentCodeRead.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intentCodeRead);
initservice.java
Intent intentService = new Intent(context, BarcodeScannerService.class);
context.startService(intentService);
application B
receive.java
BroadcastReceiver actionBarcodeScannerAndPresenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
/* Receiver for the barcode scan and presence sensor events */
if (intent.getAction().equals(Constants.ACTION_PRESENCE)) {
try
{
Log.e("Sensor Presenca","Detectada");
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Ocorreu um erro no sensor de presença", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} else if (intent.getAction().equals(Constants.ACTION_ON_BARCODE)) {
try {
String scannedText = intent.getStringExtra(Constants.BARCODE_TEXT);
if(scannedText.equals("0123456789"))
{
barcodeResult.setText(scannedText);
barcodeResult.setTextColor(getResources().getColor(R.color.green));
//Thread.sleep(5000);
ReturnResult("BARCODE", 0);
}else{
barcodeResult.setText(scannedText);
barcodeResult.setTextColor(getResources().getColor(R.color.red));
//Thread.sleep(5000);
ReturnResult("BARCODE", 1);
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(), "Ocorreu um erro ao ler o código de barras!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
This code works well.The only problem is, always when (A) send the broadcast
message the application (A) bring to front of (B). What is the flags or methods to prevent this?
My workaround for this problem was use ActivityManager
to bring to front the application that the broadcast
activity send to background. The following code was used and was adapted from Android, how to bring a Task to the foreground?
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
if (!recentTasks.get(i).baseActivity.toShortString().contains("your_class_to_be_avoid")) {
activityManager.moveTaskToFront(recentTasks.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
}
}
In this code I'm send to foreground every application that is different to my main application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With