广播接收器未使用通知按钮激活

时间:2019-01-30 00:43:45

标签: android

尝试通过按钮发出通知,即当按下按钮时,它将运行一些代码,而无需打开应用程序。但是,我的广播接收器似乎没有收到任何消息(日志从不显示我的println语句进行测试)。

public void addNotification(String title, String text, int id){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle(title)
                .setContentText(text);

        Intent notificationIntent = new Intent(this.getApplicationContext(), MainActivity.class); //REEE FIX
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 2,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        if (id == IS_RECORDING_CODE) {
            Intent btnIntent = new Intent(this.getApplicationContext(), NotificationButtonActivator.class);
            btnIntent.setAction(Intent.ACTION_MAIN);
            btnIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            btnIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingBtnIntent = PendingIntent.getActivity(getApplicationContext(), PAUSE_BUTTON_CODE, btnIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.addAction(R.drawable.pausePic, "Pause Recording", pendingBtnIntent);
        }

        builder.setContentIntent(contentIntent);


        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(id, builder.build());
    }


public class NotificationButtonActivator extends BroadcastReceiver {
    @SuppressLint("ClickableViewAccessibility")

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("NBA RAN!"); //For testing, this line is supposed to run but never does
    }
}

//In manifest application
<receiver android:name=".NotificationButtonActivator"/>

通知工作正常。但是贴在它上面的按钮显示了,但是根本不起作用(例如,广播接收器的onReceive不能在按下按钮的情况下运行)。

0 个答案:

没有答案