关于如何在通知接收器中更改setLatestEventInfo的替代方法

嗨,我在这一点得到了库存,其中“setLatesEventInfo”有错误。 我知道setLatestEventInfo不能在API 23及更高版本上运行。 有人可以帮助我如何运行此代码? 我的意思是替代方式,function相同但编码不同。 这是接收器function。 我正在通知我的Mainactivity。 这里唯一的错误是接收器,我尝试了构建器通知,但由于这是一个接收器,构建器无法应用

Receiver.java

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; /** * Created by my pc on 12/1/2015. */ public class Receiver extends BroadcastReceiver { private static final Object ACTION = "MyNotification"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION.equals(action)) { //do what you want here Variables.numMessages++; generateNotification(context,"MyNotification"); } } private void generateNotification(Context context, String message) { long when = System.currentTimeMillis(); int icon = R.drawable.ic_od_icon_24dp; NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); // String subTitle = context.getString(R.string.app_name); String subTitle = "some text"; Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.putExtra("content", message); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); notification.setLatestEventInfo(context, title, subTitle, intent); //To play the default sound with your notification: //notification.defaults |= Notification.DEFAULT_SOUND; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); } } 

MainActivity.java

 public class MainActivity extends Activity { int NOTIFICATION_ID = 1; int LED_ON_MS = 200; int LED_OFF_MS = 600; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onGenNoti(View v) { // Check readCount Variables.readCount = Variables.numMessages; PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0); if (Variables.numMessages > 0) { Notification noti = new Notification.Builder(MainActivity.this) .setTicker("You Have " + Variables.numMessages + " message") .setContentTitle("test") .setContentText("") .setSmallIcon(R.drawable.ic_od_icon_24dp) .setContentIntent(pIntent).getNotification(); noti.defaults |= Notification.DEFAULT_SOUND; noti.defaults |= Notification.DEFAULT_VIBRATE; noti.ledARGB = Color.BLUE; noti.ledOnMS = LED_ON_MS; noti.ledOffMS = LED_OFF_MS; noti.flags = Notification.FLAG_SHOW_LIGHTS; NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID, noti); } } } 

现在你必须像这样生成你的通知

  NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context); // set intent so it does not start a new activity Notification notification = builder.setContentIntent(yourPendingIntent) .setSmallIcon(icon).setWhen( System.currentTimeMillis();) .setContentTitle(title) .setContentText(message).build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(notifId, notification);