待定KitKat中的通知(4.4.4)

时间:2014-07-15 14:39:23

标签: android android-4.4-kitkat

我试图在用户点击通知时发送隐式意图。这是我的代码:

        // 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);

通知显示正常,但是当我选择它时没有任何反应。我还将Intent-filter添加到清单中,如下所示:

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

我做错了什么?非常感谢。

2 个答案:

答案 0 :(得分:2)

我相信您应该使用PendingIntent.getBroadcast代替getService,因为您没有开始提供服务。

答案 1 :(得分:2)

使用PendingIntent.getActivity()代替getService()。因为,您希望在单击通知时启动新活动。