1

My application only have MainActivity with a ImageView.

BroadcastReceiver works. The Toast Message is displayed When I connect USB.

Now, I need start my application minimized and show the application only the USB cable was connected.

BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (action.equals("usb_detect")) {
            Toast.makeText(arg0,"Atenção!",Toast.LENGTH_SHORT).show();
            finish();
            startActivity(getIntent());
            //startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));
        }
    }
};

Manifest is here: https://i.stack.imgur.com/sa3Pe.jpg

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Please specifically ask what you are looking for help with. Is the question "How do I start my application minimized and show the application only the USB cable is connected"? Also, please provide samples of what you have tried already. – ToddR Oct 23 '18 at 20:20
  • I have a button to hide the application (moveTaskToBack(true)). I need open the application again when the USB connection was received. – Luiz Lorena Oct 24 '18 at 11:19

1 Answers1

0

To bring your application to the foreground, add the following to onReceive():

Intent launchIntent = getPackageManager().
    getLaunchIntentForPackage("your.package.name");
startActivity(launchIntent);

This will not start a new Activity, it will simply bring the existing task containing your application to the foreground in whatever state it was in when it got moved to the background.

Also, remove the call to finish() from your BroadcastReceiver. finish() is only used on an Activity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Not work. Only the Toast is show. if (action.equals("usb_detect")) { Toast.makeText(arg0,"Atenção!",Toast.LENGTH_SHORT).show(); Intent launchIntent = getPackageManager(). getLaunchIntentForPackage("br.com.compex.testeimageviewgif"); startActivity(launchIntent); – Luiz Lorena Oct 24 '18 at 13:59
  • Please add your manifest to your question. Edit the question and paste in your manifest. – David Wasser Oct 24 '18 at 14:03
  • Thanks David. I put a picture. Is visible to you? – Luiz Lorena Oct 24 '18 at 14:27
  • Try changing `startActivity()` to `arg0.getApplicationContext().startActivity()` instead. If that doesn't work please add your current code to your question. – David Wasser Oct 24 '18 at 14:45