关闭应用程序时,请在通知按钮上打开活动

发布于 2025-01-22 01:23:21 字数 1171 浏览 0 评论 0 原文

我正在尝试打开 mainActivity 当用户单击我的通知中的按钮时,该应用仅在后台运行,而使用服务。单击按钮时,这些行是在 service 类中触发的:

Intent openApp = new Intent(this, MainActivity.class);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openApp);

我已经检查了它,并触发了行,因此对按钮的单击,活动没有任何问题不会打开。

有什么建议吗?为什么这对我不起作用?如何使它起作用?

编辑

我被要求提供更多代码,因此在我的 onstartcommand()中,我的 service 在其 intent oint intent >,我调用 killService()方法(杀死 service )启动 mainActivity 并执行其他事情:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
    killService();
}

设置 Notification s按钮,我使用此代码:

Intent stopActionIntent = new Intent(this, TimerService.class);
        stopActionIntent.setAction(ACTION_STOP_SERVICE);
        PendingIntent stopActionPendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);

timerNotificationBuilder.addAction(R.drawable.stop, "Stop", stopActionPendingIntent);

正如我所说,该按钮已经对用户单击它做出反应,因此这不是问题。

I'm trying to open the MainActivity when the user clicks a button in my notification, while the app is only running in the background with a service. When the button is clicked, these lines are triggered in the Service class:

Intent openApp = new Intent(this, MainActivity.class);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openApp);

I've checked it, and the lines are triggered, so there's no problem in reacting to the button's click, the Activity won't open though.

Any suggestions? Why isn't this working for me and how can I make it work?

Edit

I was asked for some more code, so in my onStartCommand() inside my Service, if it starts with a stop-action within its intent, I call the killService() method, which kills the Service, starts the MainActivity and do some other stuff:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
    killService();
}

To set the Notifications button, I use this code:

Intent stopActionIntent = new Intent(this, TimerService.class);
        stopActionIntent.setAction(ACTION_STOP_SERVICE);
        PendingIntent stopActionPendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);

timerNotificationBuilder.addAction(R.drawable.stop, "Stop", stopActionPendingIntent);

And as I said, the button already reacts to the user clicking on it, so that's not the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖伴 2025-01-29 01:23:22

您可以尝试在 broadcastreceiver 中接收单击,然后从那里打开活动。

  1. 尝试此操作按钮o您的通知:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");

screateNotificationalactionbutton 方法是:

public NotificationCompat.Action createNotificationActionButton(String text){
        Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);

        @SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);

        return new NotificationCompat.Action(0, text, pendingIntent);
    }
  1. 创建一个名为 stopwatchnotificationAcceAcceReceiver 的类,使其范围a broadcastreceiver 。这是该类的代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class StopwatchNotificationActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PrefUtil.setIsRunningInBackground(context, false);
        PrefUtil.setTimerSecondsPassed(context, 0);
        PrefUtil.setWasTimerRunning(context, false);
        context.stopService(MainActivity.serviceIntent);
        Intent activityIntent = new Intent(context, MainActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActvity(activityIntent);
    }
}

您还需要在这样的清单中注册该接收器:

<receiver android:name="StopwatchNotificationActionReceiver"/>
  1. 其中 mainActivity.ServiceIntent 是一个看起来像这样的公共静态变量:
public static Intent serviceIntent;

此意图仅用于以这样的启动开始服务:

//In onCreate
serviceIntent = new Intent(this, TimerService.class);

//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent);
            }

或者您可以尝试简单的方法:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
    Context context = this;
    Intent activityIntent = new Intent(context, MainActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActvity(activityIntent);
    killService();
}

编辑


另一个解决方案在这里。再次。您需要参考我的 repo ,因为我对文件进行了更改以进行更改以进行更改完成您的任务。 In the service class, refer to

与往常一样,您可以查看app 的结果。

我希望代码能够完成您的工作。

You can try to receive the click in a BroadcastReceiver and then open activity from there.

  1. Try this to add a action button o your notification:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");

Where the createNotificationActionButton method is this:

public NotificationCompat.Action createNotificationActionButton(String text){
        Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);

        @SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);

        return new NotificationCompat.Action(0, text, pendingIntent);
    }
  1. Create a class named StopwatchNotificationActionReceiver and make it extent a BroadcastReceiver. This is the code for that class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class StopwatchNotificationActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PrefUtil.setIsRunningInBackground(context, false);
        PrefUtil.setTimerSecondsPassed(context, 0);
        PrefUtil.setWasTimerRunning(context, false);
        context.stopService(MainActivity.serviceIntent);
        Intent activityIntent = new Intent(context, MainActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActvity(activityIntent);
    }
}

Also you need to register that receiver in your manifest like this:

<receiver android:name="StopwatchNotificationActionReceiver"/>
  1. Where the MainActivity.serviceIntent is a public static variable which looks like this:
public static Intent serviceIntent;

And this intent is only used to start the service like this:

//In onCreate
serviceIntent = new Intent(this, TimerService.class);

//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent);
            }

Or you can try the simple method:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
    Context context = this;
    Intent activityIntent = new Intent(context, MainActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActvity(activityIntent);
    killService();
}

Edit


Another solution is here. Again. You need to refer to my repo as I have made changes to the files in order to complete your task. In the service class, refer to this method. There, I start the activity if the action is reset(r). Or else, it opens the broadcast receiver. Then, in the activity, I receive that extra in the onResume() method. If the reset button is not clicked, it opens the Receiver class.

And as always, you can view the result of the app from here.

I hope that code will do your work.

oО清风挽发oО 2025-01-29 01:23:22

我找到了!参见这个答案
该答案表明启用 screanoverlay 设置,因为从Android 10开始,然后您才能通过调用我使用过的行来从后台打开活动。

为了使它起作用,您必须通过 subtest.xml >:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

然后,用户必须通过其他应用程序启用显示设置。
我搜索了一个选项,使用户更容易地进入此设置,并找到此答案
该答案给出了将用户重定向到其他应用程序设置的显示的代码

if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 0);
}

,然后我用通知的内容(文本)指导用户有关如何启用设置。

设置启用后,我在工作前使用的行

没有完全解决

上述工作的整个配置,但前提是该应用没有被杀死。
如果该应用被杀死,我尝试了上面列出的方法,则该应用程序加入了最近的应用程序列表,但不会打开并显示。
解决此问题的解决方案将被视为答案,而不是这个问题。

I found it! See this answer.
This answer suggests enabling ScreeanOverlay settings because as of Android 10 and later you can no longer open an activity from the background just by calling the lines I've used.
To make it work, you'd have to add this permission through your Manifest.xml:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

And then the user would have to enable the Display over other apps setting.
I searched for an option to get the user to this setting more easily and found this answer.
This answer gives a code that redirects the user to the Display over other apps setting

if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 0);
}

and then I guide the user with the notification's content (text) on how to enable the setting.
Once the setting is enabled, The lines I've used before work.\

So, problem solved?

Not Completely Solved

this whole configuration described above works, but only if the app is not killed.
If the app was killed and I try the method listed above, the app joins the recent apps list, but won't open and show up.
A solution that solves this issue as well will be accepted as an answer instead of this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文