Android 前台服务的 PendingIntent

发布于 2024-11-29 00:15:14 字数 2366 浏览 0 评论 0原文

我仍然对在前台启动服务感到非常困惑。

目前:我有一个活动在后台启动一个服务来对传感器数据+gps 数据执行统计。它不断被杀死,所以我在前台启动它。工作正常。 但是,当我触摸状态栏中的通知图标时,它似乎启动了一个新活动(我在“待处理意图”中做了说明)。

简而言之,我仍然对整件事感到困惑。 我希望有一个活动在前台启动服务,当您单击通知栏时,它将启动该服务的现有活动带到前台。

以下是活动中的一些代码(当前将 PendingIntent 设置为 null):

private ServiceConnection mConnection = new ServiceConnection() {
     public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service. Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.

        cycleDataService = ((CycleDataProService.LocalBinder) service)
                .getService();
        //Todo the notification bullshaite here...
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.icon;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";          
        //Intent notificationIntent = new Intent(cycleDataService, CycleDataProService.class);
        //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, myOwnIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pi = PendingIntent.getActivity(context, 0, null, 0);

        //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, , 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, pi);
        mNotificationManager.notify(HELLO_ID, notification);
        cycleDataService.startForeground(HELLO_ID, notification);
        /*cycleDataService.startService(new Intent(cycleDataService,
                CycleDataProService.class));*/
        serviceIsRunning = true;
        // Tell the user about this for our demo.
        // Toast.makeText(Binding.this, R.string.local_service_connected,
        // Toast.LENGTH_SHORT).show();
    }

I am still thoroughly confused by starting a service in the foreground.

Currently: I have an activity that starts a service in the background to perform stats on sensor data+gps data. It keeps getting killed, so I started it in the foreground. Works fine.
However when I touch on the notification icon in the status bar it seems to start a new activity (which I did state in the Pending Intent).

So in a nutshell I am still confused with the whole thing.
I would like an activity to start a service in the foreground, and when you click on the notification bar it brings the existing activity that started the service to the front.

Here is some code within the activity (currently set PendingIntent to null):

private ServiceConnection mConnection = new ServiceConnection() {
     public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service. Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.

        cycleDataService = ((CycleDataProService.LocalBinder) service)
                .getService();
        //Todo the notification bullshaite here...
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.icon;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";          
        //Intent notificationIntent = new Intent(cycleDataService, CycleDataProService.class);
        //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, myOwnIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pi = PendingIntent.getActivity(context, 0, null, 0);

        //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, , 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, pi);
        mNotificationManager.notify(HELLO_ID, notification);
        cycleDataService.startForeground(HELLO_ID, notification);
        /*cycleDataService.startService(new Intent(cycleDataService,
                CycleDataProService.class));*/
        serviceIsRunning = true;
        // Tell the user about this for our demo.
        // Toast.makeText(Binding.this, R.string.local_service_connected,
        // Toast.LENGTH_SHORT).show();
    }

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

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

发布评论

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

评论(2

记忆で 2024-12-06 00:15:14

使用这种方式:

Intent notificationIntent = new Intent(context, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notification.setLatestEventInfo(context, "title", null, contentIntent);

use this way:

Intent notificationIntent = new Intent(context, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notification.setLatestEventInfo(context, "title", null, contentIntent);
深空失忆 2024-12-06 00:15:14

如果您希望 Activity 继续而不是重新启动,请使用 FLAG_ACTIVITY_SINGLE_TOP。

Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);

Use FLAG_ACTIVITY_SINGLE_TOP if you want your activity to continue, and not restart.

Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.setLatestEventInfo(context, "title", null, contentIntent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文