首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于foreGround服务的安卓PendingIntent

用于foreGround服务的安卓PendingIntent
EN

Stack Overflow用户
提问于 2011-08-10 20:14:47
回答 2查看 3.2K关注 0票数 0

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

目前:我有一个在后台启动服务以对传感器data+gps数据执行统计的活动。它一直被杀死,所以我在前台启动了它。工作正常。但是,当我触摸状态栏中的通知图标时,它似乎启动了一个新的活动(我确实在待定意图中声明了这一点)。

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

下面是activity中的一些代码(当前将PendingIntent设置为空):

代码语言:javascript
运行
复制
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();
    }
EN

回答 2

Stack Overflow用户

发布于 2011-08-10 20:20:03

使用以下方法:

代码语言:javascript
运行
复制
Intent notificationIntent = new Intent(context, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notification.setLatestEventInfo(context, "title", null, contentIntent);
票数 2
EN

Stack Overflow用户

发布于 2014-12-23 07:51:59

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

代码语言:javascript
运行
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7010598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档