为什么从服务通知中调用待处理的意图不会启动活动

问题描述 投票:0回答:1

在我的Github帐户上,我做了一个简单的项目来描述我的问题here my project

我的主活动有一个按钮,其onClick方法调用后台服务。Backgroundservice等待5秒钟,然后触发通知。

这里是BackgroundService的代码(您可以在Github上找到

@Override
    protected void onHandleIntent(Intent intent) {//methode appelée en arrière plan
        if (intent != null) {

            try{
                Thread.sleep(5000);
            } catch (Exception e){
                e.printStackTrace();
            }

            mNotificationsManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            CharSequence tickerText = "view hike";
            Intent intentReceiverAcctivity = new Intent(TAG_INTENT);
            intentReceiverAcctivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intentReceiverAcctivity, 0);

            Notification.Builder builder = new Notification.Builder(this);

            builder.setAutoCancel(false);
            builder.setTicker(tickerText);
            builder.setContentTitle("The Hike");
            builder.setContentText("HikeMap is available");
            builder.setSmallIcon(R.drawable.explore);
            Bitmap large_icon_bmp = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.ic_action_map);
            builder.setLargeIcon(large_icon_bmp);
            builder.setContentIntent(pendingIntent);
            builder.setOngoing(true);
            builder.setSubText("Click on this icon to access the Hike's map");   //API level 16
            builder.setNumber(5000); //durée d'apparition de l'icone dans la barre de notifications ?
            builder.build();

            Notification notification = builder.getNotification();
            mNotificationsManager.notify(11, notification);

        }
    }

展开通知栏,然后单击large_icon按钮,将启动另一个名为ReceiverActivity的活动,其唯一目的是显示不同的消息。

在模拟器上,小图标出现在通知栏上,当我展开该栏时,我可以看到大图标,但是单击它不会触发ReceiverActivity

在我的智能手机上,通知栏上没有小图标,扩展的通知区域上没有大图标...

关于服务代码出了什么问题的任何想法?该小示例的完整代码可在GitHub ...

中找到

感谢您的帮助!

android android-activity android-service android-notifications
1个回答
0
投票

Intent intentReceiverAcctivity =新的Intent(TAG_INTENT);

public final static String TAG_INTENT = "NotificationClicked";

[抱歉,您没有设置要运行的活动。您应该将Intent与两个参数一起使用,例如

Intent intent = new Intent(Context, YourActivity.class);
© www.soinside.com 2019 - 2024. All rights reserved.