我有以下代码在网络状态发生变化时发出通知。 但当我改变网络状态时,应用程序停止工作! 请帮我做通知。
我确定问题出在addMyNotification函数中。但不知道如何解决它。
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent broadcast) {
addMyNotification("hi","hello",1,context);
}
private void addMyNotification(String title,String message,int id ,Context context ) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, SendFood.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
manager.notify(id, builder.build());
}