gpt4 book ai didi

java - 单击通知中的按钮不会触发广播接收器

转载 作者:行者123 更新时间:2023-12-01 19:02:22 24 4
gpt4 key购买 nike

我创建了一个 Android 应用程序,我想在单击通知上的按钮时显示 toast ,但不知何故它对我不起作用,有人可以帮忙吗?以下是我在应用程序中使用的三个文件。提前致谢

MainActivity.Java

public class MainActivity extends AppCompatActivity {
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
private int notification_id;
private RemoteViews remoteViews;
private Context context;

private final String CHANNEL_ID = "personal_notifications";
private final int NOTIFICATION_ID = 001;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context=this;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
remoteViews= new RemoteViews(getPackageName(),R.layout.custom_notification);

notification_id = (int)System.currentTimeMillis();
Intent buttonIntent = new Intent("button_clicked");
buttonIntent.putExtra("id",notification_id);
PendingIntent p_button_intent = PendingIntent.getBroadcast(context,123,buttonIntent,0);
remoteViews.setOnClickPendingIntent(R.id.button, p_button_intent);

findViewById(R.id.button_show_notif).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNotificationChannel();
Intent notification_intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notification_intent, 0);

builder = new NotificationCompat.Builder(context,CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_sms)
.setAutoCancel(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomBigContentView(remoteViews)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
notificationManager.notify(notification_id,builder.build());

}
});
}

button_listner

package com.red.testbroadcastreceivers;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class button_listner extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getExtras().getInt("id"));
Toast.makeText(context, "Working", Toast.LENGTH_LONG).show();
}
}

AndroidManifest

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:enabled="true" android:name="button_listner">
<intent-filter>
<action android:name="button_clicked"/>
</intent-filter>
</receiver>

我还有另一个用于通知设计的custom_notificatoin.xml,我没有在此处添加。

最佳答案

现在无法在 AndroidManifest 上静态注册接收器,您必须通过代码上的 context.registerReceiver 方法在代码上注册

https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)

关于java - 单击通知中的按钮不会触发广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com