【问题标题】:Hide / Unhide application when connect / disconnect USB BroadcastReceiver连接/断开 USB BroadcastReceiver 时隐藏/取消隐藏应用程序
【发布时间】:2019-03-28 03:44:14
【问题描述】:

我的应用程序只有MainActivityImageView

BroadcastReceiver 有效。当我连接 USB 时显示 Toast 消息。

现在,我需要最小化启动我的应用程序,并显示应用程序只连接了 USB 电缆。

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));
        }
    }
};

清单在这里:https://i.stack.imgur.com/sa3Pe.jpg

【问题讨论】:

  • 请具体询问您在寻求什么帮助。问题是“如何最小化启动我的应用程序并仅显示连接了 USB 电缆的应用程序”?另外,请提供您已经尝试过的示例。
  • 我有一个隐藏应用程序的按钮 (moveTaskToBack(true))。收到 USB 连接后,我需要再次打开应用程序。

标签: android android-intent broadcastreceiver android-task


【解决方案1】:

要将您的应用程序置于前台,请将以下内容添加到onReceive()

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

这不会启动一个新的Activity,它只会将包含您的应用程序的现有任务以其移动到后台时的任何状态带到前台。

另外,从您的BroadcastReceiver 中删除对finish() 的调用。 finish() 仅用于Activity

【讨论】:

  • 不工作。只显示吐司。 if (action.equals("usb_detect")) { Toast.makeText(arg0,"Atenção!",Toast.LENGTH_SHORT).show();意图launchIntent = getPackageManager()。 getLaunchIntentForPackage("br.com.compex.testeimageviewgif"); startActivity(launchIntent);
  • 请将您的清单添加到您的问题中。编辑问题并粘贴到您的清单中。
  • 谢谢大卫。我放一张图。你可以看到吗?
  • 尝试将 startActivity() 改为 arg0.getApplicationContext().startActivity()。如果这不起作用,请将您当前的代码添加到您的问题中。
猜你喜欢
  • 2013-10-07
  • 2012-05-04
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多