简体   繁体   English

在KitKat中等待通知的意图(4.4.4)

[英]Pending Intent with Notifications in KitKat (4.4.4)

I'm trying to get the Implicit Intent sent when the user taps on a notification. 我试图在用户点击通知时发送隐式意图。 This is my code: 这是我的代码:

        // Intent used to share data
        Intent notificationIntent = new Intent(Intent.ACTION_SEND);
        notificationIntent.setType("text/plain");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Intent message to share
        String intentString = "I took " + stepsAlarm.getText() + " steps";
        notificationIntent.putExtra(Intent.EXTRA_TEXT, intentString);

        // Create a pending intent that triggers only when the notification is selected
        PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, 0);

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        //Resources res = context.getResources();
        Notification.Builder builder = new Notification.Builder(context);

        builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.small_not)
                //.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker("Stepmeter")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Stepmeter Alert")
                .setContentText("You took " + stepsAlarm.getText() + " steps");
        Notification n = builder.build();

        nm.notify(7, n);

The notification shows up just fine, but when I select it nothing happens. 通知显示很好,但是当我选择它时,什么也没有发生。 I also added the Intent-filter to the manifest like so: 我还向清单中添加了Intent过滤器,如下所示:

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="text/plain" />
        </intent-filter>

What am I doing wrong? 我究竟做错了什么? Thanks much. 非常感谢。

我相信您应该使用PendingIntent.getBroadcast而不是getService因为您没有启动服务。

Use PendingIntent.getActivity() instead of getService() . 使用PendingIntent.getActivity()而不是getService() Since, you want to start a new activity on clicking the notification. 从那以后,您要在单击通知时开始新的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM