两个按钮,一个活动

时间:2018-11-26 20:11:47

标签: android android-pendingintent

我的通知中有两个按钮,它们打开相同的活动,但作为额外的不同数据发送。我真的需要创建2个待定意图和2个意图吗?也许有一些简短的版本?

        Intent intent1 = new Intent(this, ResponseActivity.class); //same
        intent1.putExtra("RES","a");
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP   | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent2 = new Intent(this, ResponseActivity.class); //same
        intent2.putExtra("RES","b"); //but different
        intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP   | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .addAction(0, "A", pendingIntent1) 
                .addAction(0, "B", pendingIntent2) 
                .setLargeIcon(bitmap)
                .setContentTitle(userDB.getName())
                .setContentText(smallText)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

3 个答案:

答案 0 :(得分:1)

您的代码有问题。您的代码最终将在两个按钮上设置相同的Private Sub InsertRecord() Dim constr As String = "server=localhost;user=root;database=login;port=3306;password=root123;SslMode=none" 'A Using...End Using block will ensure that your data objects 'are closed and disposed event if there is an error Try Using conn As MySqlConnection = New MySqlConnection(constr) 'You need the new keyword to create the command 'Pass the sql query string and the connection object to the 'constructor of the command 'Create the command once, only the value of the subjectpriority changes Using cmd As New MySqlCommand("INSERT INTO mastersubject (name, subjectpriority) VALUES (@name, @subjectpriority);", conn) cmd.Parameters.AddWithValue("@name", ComboBox1.SelectedItem.ToString) cmd.Parameters.Add("@subjectpriority") 'Open the connection as late as possible conn.Open() For i = 0 To CheckedListBox1.CheckedItems.Count - 1 Dim Result As Integer 'You are not adding a new parameter, just changing its value cmd.Parameters("@subjecpriority").Value = CheckedListBox1.CheckedItems(i).ToString() 'the command will be executed for each item checked Result = cmd.ExecuteNonQuery() If Result > 0 Then MessageBox.Show("Record has been saved") Else MessageBox.Show("Error!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End If Next End Using 'Disposes the command End Using ' closes and disposes the connection Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub 和相同的PendingIntent。这是因为在比较Intent以确定给定Intent是否已经存在PendingIntent时不考虑“附加”。在您的情况下,两个Intent看起来一样,因此对Intent的两个调用将返回相同的PendingIntent.getActivity()

您需要将代码更改为如下形式:

PendingIntent

我用一个 Intent intent = new Intent(this, ResponseActivity.class); //same intent.putExtra("RES","a"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Update the "extra" in the Intent intent.putExtra("RES","b"); //but different PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT); 产生了两个不同的Intent。这里的关键是为PendingIntent使用不同的值(这是requestCode的第二个参数)。这样可以确保您获得2个不同的PendingIntent.getActivity()

答案 1 :(得分:0)

“意图”不是打开活动的调用,实际上实际上是向活动发送的消息(在大多数情况下,该消息是启动活动的消息)。

因此,不幸的是,如果您想要两个不同的消息,则需要两个不同的意图。

答案 2 :(得分:-1)

考虑JPanel会返回putEtra(),您可以像这样缩短代码:

Intent