我创建了一个android应用程序,我想在该窗口上单击Notification上的一个按钮来显示祝酒词,但是它对我不起作用,有人可以帮忙吗?
以下是我在应用程序中使用的三个文件。
提前致谢

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();
    }
}


Android清单

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

10-14 12:27