Android通知操作按钮

时间:2015-04-13 10:18:06

标签: android

我在“通知”中尝试操作,单击“构建器”中的按钮,但我不能。

我阅读了官方文件:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Notification notification = new Notification.Builder(context)
    // Show controls on lock screen even when user hides sensitive content.
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.ic_stat_player)

    // Add media control buttons that invoke intents in your media service
    .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0 HERE A BUTTON
    .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1 HERE A BUTTON
    .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2 HERE A BUTTON

    // Apply the media style template
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1 /* #1: pause button */)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("Wonderful music")
    .setContentText("My Awesome Band")
    .setLargeIcon(albumArtBitmap)
    .build();

我看到这篇文章:

Android, get Notifications Responses

有人说:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Notification n = sbn.getNotification();
    for(Action action : n.actions){
        String title = action.title.toString;
        ...
    }
}

但我无法使用,因为我没有扩展“NotificationListenerService”我扩展了IntentService

如何点击按钮以及发生事件后?

任何人都可以知道NotificationListenerService和IntentService的区别吗?

我希望能正确解释

2 个答案:

答案 0 :(得分:1)

我创建了自定义消息的自定义通知和通知的可绘制图像,并在用户单击通知以打开新屏幕时添加了处理程序。

private void generateNotification(Context context, String message,
        String pid, String type) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();

    String title = context.getString(R.string.app_name);



        Intent notificationIntent = new Intent(context,
                AddFriendRequestActivity.class);
        notificationIntent.putExtra("screen", "noti");
        notificationIntent.putExtra("message", message);
        notificationIntent.putExtra("friendId", pid);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_ONE_SHOT);

        Notification notification = new NotificationCompat.Builder(this)
                .setContentIntent(intent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("MeetAtAirport").setContentText(message).build();
        notification.setLatestEventInfo(context, title, message, intent);
        RemoteViews contentView = new RemoteViews(this.getPackageName(),
                R.layout.custom_notification);// set your custom layout
        contentView
                .setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "MeetAtAirport");
        contentView.setTextViewText(R.id.text, message);
        notification.bigContentView = contentView;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int noteId = 1232;

        notificationManager.notify(noteId, notification);


}

有一个通知布局,名称为R.layout.custom_notification:

`

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="10dp"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/title"
    style="@style/txtHelvBold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"
    android:textSize="20dp" />

<TextView
    android:id="@+id/text"
    style="@style/txtHelvBold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_toRightOf="@id/image" />

`

在此布局文件中您可以添加按钮并处理上面代码中使用按钮的“其他点击”。

答案 1 :(得分:0)

//Exemple of notification with Button
private static void scheduleNotificationWithButton(Context context, String message) {

    int notifReCode = 1;

    //What happen when you will click on button
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);

    //Button
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();

    //Notification
    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Back to Application ?")
            .setContentTitle("Amazing news")
            .addAction(action) //add buton
            .build();

    //Send notification
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}