广播接收器未接收到意图(RemoteViews/PendingIntent)
Broadcast Receiver not receiving intent (RemoteViews / PendingIntent)
我正在处理包含两个按钮的自定义通知。我正在使用 RemoteViews 并使用了 PendingIntent,以便在广播接收器中接收它。它在华为设备上工作正常但在 Oppo 和 Infinix 设备上不工作。
我正在 manifest 中像这样注册广播接收器:
<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>
在 Main Activity 中使用这些方法进行通知:
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(), 0, 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(), 0, 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,0,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,0,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中启动通知服务并为电池信息注册广播接收器Activity:
//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();
}
}
谁能帮帮我?我不知道我做错了什么
在您的广播接收器中,您必须检查操作而不是 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();
}
}
}
我发布答案是为了帮助其他人。我正在使用 隐式意图 作为广播接收器,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(), 0, 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);
}
我正在处理包含两个按钮的自定义通知。我正在使用 RemoteViews 并使用了 PendingIntent,以便在广播接收器中接收它。它在华为设备上工作正常但在 Oppo 和 Infinix 设备上不工作。
我正在 manifest 中像这样注册广播接收器:
<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>
在 Main Activity 中使用这些方法进行通知:
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(), 0, 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(), 0, 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,0,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,0,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中启动通知服务并为电池信息注册广播接收器Activity:
//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();
}
}
谁能帮帮我?我不知道我做错了什么
在您的广播接收器中,您必须检查操作而不是 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();
}
}
}
我发布答案是为了帮助其他人。我正在使用 隐式意图 作为广播接收器,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(), 0, 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);
}