广播接收器未接收到意图RemoteViews / PendingIntent

如何解决广播接收器未接收到意图RemoteViews / PendingIntent

我正在处理包含两个按钮的自定义通知。我正在使用RemoteViews,并使用了PendingIntent,以便它将在广播接收器中接收。在华为设备上可以正常工作,但在Oppo和Infinix设备上不能正常工作。

我正在清单注册这样的广播接收器:

<receiver android:name="com.appedia.flashlight.torch.Notification_Receiver">
            <intent-filter>
                <action android:name="Notification_Flashlight"></action>
                <action android:name="Notification_SOS"></action>
                <action android:name="Notification_Delete"></action>
            </intent-filter>
        </receiver>

主要活动中使用以下方法进行通知

public void Notification()
        {
            builder = new NotificationCompat.Builder(this);
            builder.setAutoCancel(true)
                    .setCustomContentView(remoteViews)
                    .setDeleteIntent(delete_pending_intent)
                    .setContentIntent(pendingIntent);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
                builder.setSmallIcon(R.drawable.flashlight);
            }
            else
            {
                builder.setSmallIcon(R.mipmap.ic_launcher);
            }
    
    
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            {
                int importance = notificationmanager.IMPORTANCE_HIGH;
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_NAME,importance);
                assert notificationmanager != null;
                builder.setChannelId(NOTIFICATION_CHANNEL_ID);
                notificationmanager.createNotificationChannel(notificationChannel);
            }
    
            assert notificationmanager != null;
        }
    
        public void NotificationItemsClickIntent()
        {
            Intent notification_intent = new Intent(getApplicationContext(),MainActivity.class);
            pendingIntent = PendingIntent.getActivity(getApplicationContext(),notification_intent,0);
    
            Intent notification_deleted_intent = new Intent("Notification_Delete");
            notification_deleted_intent.putExtra("notification_delete","notification_delete");
            delete_pending_intent = PendingIntent.getbroadcast(getApplicationContext(),notification_deleted_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    
    
            /* Pending Intent for Notification Flashlight Button */
            //Intent with flashlight button for triggering broadcast Receiver when clicked from notification bar
            Intent notification_flashlight_intent = new Intent("Notification_Flashlight");
            notification_flashlight_intent.putExtra("notification_flashlight","Flashlight");
    
            //start flashlight when notification bar button is clicked
            PendingIntent notification_flashlight_button = PendingIntent.getbroadcast(this,notification_flashlight_intent,PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setonClickPendingIntent(R.id.notification_flashlight_button,notification_flashlight_button);
    
            /* Pending Intent for Notification SOS Button */
            //Intent with sos button for triggering broadcast Receiver when clicked from notification bar
            Intent notification_sos_intent = new Intent("Notification_SOS");
            notification_sos_intent.putExtra("notification_sos","SOS");
    
            //start flashlight when notification bar button is clicked
            PendingIntent notification_sos_button = PendingIntent.getbroadcast(this,notification_sos_intent,PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setonClickPendingIntent(R.id.notification_sos_button,notification_sos_button);
        }
    
        //broadcast receiver for getting battery percentage
        private broadcastReceiver mBatInfoReceiver = new broadcastReceiver(){
            @Override
            public void onReceive(Context ctx,Intent intent) {
    
                boolean isPlugged;
    
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
                remoteViews.setTextViewText(R.id.notification_battery_percent,String.valueOf(level) + "%");
    
                int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
                isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    
                if(isPlugged)
                {
                    remoteViews.setTextViewText(R.id.notification_battery_text,"Charging");
                }
                else
                {
                    remoteViews.setTextViewText(R.id.notification_battery_text,"discharging");
                }
    
                NotificationItemsClickIntent();
                Notification();
                notificationmanager.notify(0,builder.build());

                unregisterReceiver(mBatInfoReceiver);
                }
            };

在MainActivity的onCreate中启动通知服务并为电池信息注册广播接收器:

//Starting Notification Service
        notificationmanager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);
        remoteViews = new RemoteViews(getPackageName(),R.layout.notification_bar);

    //for getting battery percentage (shows notification)
        this.registerReceiver(this.mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    
              

通知广播接收器:

public class Notification_Receiver extends broadcastReceiver {

String notification_flashlight,notification_sos,notification_delete;

@Override
public void onReceive(Context context,Intent intent) {

    notification_flashlight = intent.getExtras().getString("notification_flashlight");
    notification_sos = intent.getExtras().getString("notification_sos");
    notification_delete = intent.getExtras().getString("notification_delete");

    if (notification_flashlight != null) {

        Toast.makeText(context,"Flashlight",Toast.LENGTH_SHORT).show();
    }

    else if (notification_sos != null) {

        Toast.makeText(context,"SOS",Toast.LENGTH_SHORT).show();
    }

    else if (notification_delete != null) {

        Toast.makeText(context,"Delete",Toast.LENGTH_SHORT).show();
       
    }
}

有人可以帮助我吗?我不知道我在做什么错

解决方法

我正在发布答案,这样也会对其他人有所帮助。我对广播接收器使用了隐式意图,而Android Oreo不建议这样做。因此,我必须使用明确意图,然后它才能开始正常工作。

Manifest.xml:

<receiver android:name="com.appedia.flashlight.torch.Notification_Receiver" />

MainActivity.java:

public void NotificationItemsClickIntent()
{
    Intent notification_intent = new Intent(this,MainActivity.class);
    pendingIntent = PendingIntent.getActivity(getApplicationContext(),notification_intent,0);

    Intent notification_deleted_intent = new Intent(this,Notification_Receiver.class);
    notification_deleted_intent.putExtra("notification_delete","notification_delete");
    delete_pending_intent = PendingIntent.getBroadcast(this,1,notification_deleted_intent,PendingIntent.FLAG_UPDATE_CURRENT);

    /* Pending Intent for Notification Flashlight Button */
    //Intent with flashlight button for triggering Broadcast Receiver when clicked from notification bar
    Intent notification_flashlight_intent = new Intent(this,Notification_Receiver.class);
    notification_flashlight_intent.putExtra("notification_flashlight","Flashlight");
    PendingIntent notification_flashlight_button = PendingIntent.getBroadcast(this,2,notification_flashlight_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.notification_flashlight_button,notification_flashlight_button);

    /* Pending Intent for Notification SOS Button */
    //Intent with sos button for triggering Broadcast Receiver when clicked from notification bar
    Intent notification_sos_intent = new Intent(this,Notification_Receiver.class);
    notification_sos_intent.putExtra("notification_sos","SOS");
    PendingIntent notification_sos_button = PendingIntent.getBroadcast(this,3,notification_sos_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.notification_sos_button,notification_sos_button);
}
,

在广播接收器中,您必须检查操作而不是getExtras.getString()

public class Notification_Receiver extends BroadcastReceiver {

String notification_flashlight,notification_sos,notification_delete;

@Override
public void onReceive(Context context,Intent intent) {

    notification_flashlight = intent.getAction() == "notification_flashlight";
    notification_sos =        intent.getAction() == "notification_sos";
    notification_delete =     intent.getAction() == "notification_delete";

    if (notification_flashlight != null && notification_flashlight == true) {

        Toast.makeText(context,"Flashlight",Toast.LENGTH_SHORT).show();
    }

    else if (notification_sos != null && notification_sos== true) {

        Toast.makeText(context,"SOS",Toast.LENGTH_SHORT).show();
    }

    else if (notification_delete != null && notification_delete == true) {

        Toast.makeText(context,"Delete",Toast.LENGTH_SHORT).show();
       
     }
   }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?