<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(), 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);
}
};
//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奥利奥不建议的。所以,我必须使用明确的意图,然后它开始运作良好。
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);
}
我有一个很奇怪的问题。 我正在发送广播并设置一些额外内容,但接收者没有收到: 发送: 并收到: 由于某些原因,downloadID为空。有什么提示吗? 谢谢
问题内容: 有人可以解释和之间的确切区别吗? 在什么情况下我们必须使用每个Receiver类? 问题答案: 和之间只有一个区别。 当您收到内部广播方法时, 假设, BroadcastReceiver : 它 不保证 该 CPU将保持清醒 ,如果你启动一些长时间运行的进程。CPU可能会立即回到睡眠状态。 WakefulBroadcastReceiver : 这是 保证 该 CPU将保持清醒 ,直到你
4.2.1.3 内部广播接收器 内部广播接收器是广播接收器,它将永远不会收到从内部应用以外发送的任何广播。 它由几个内部应用组成,用于保护内部应用处理的信息或功能。 要点(接收广播): 定义内部签名权限来接收广播。 声明使用内部签名权限来接收结果。 将导出属性显式设置为true。 需要静态广播接收器定义的内部签名权限。 需要内部签名来注册动态广播接收器。 确认内部签名权限是由内部应用定义的。 尽管
4.2.1.2 公共广播接收器 公共广播接收器是可以从未指定的大量应用程序接收广播的广播接收器,因此有必要注意,它可能从恶意软件接收广播。 要点(接收广播): 将导出属性显式设为true。 小心并安全地处理收到的意图。 返回结果时,不要包含敏感信息。 公共广播接收器的示例代码可以用于静态和动态广播接收器。 PublicReceiver.java package org.jssec.android.
4.2.1.1 私有广播接收器 私人广播接收器是最安全的广播接收器,因为只能接收到从应用内发送的广播。 动态广播接收器不能注册为私有,所以私有广播接收器只包含静态广播接收器。 要点(接收广播): 将导出属性显示设为false 小心并安全地处理收到的意图,即使意图从相同的应用中发送 敏感信息可以作为返回结果发送,因为请求来自相同应用 AndroidManifest.xml <?xml version