android studio sdk 升级到23后 setLatestEventInfo被完全弃用api11 之前的机型可以使用v4包下的NotificationCompat实现
import android.support.v4.app.NotificationCompat;
NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder notifyBuilder; Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);notifyBuilder = new NotificationCompat.Builder(this) .setLargeIcon(bitmap)/*设置large icon*/
.setSmallIcon(R.drawable.icon)/*设置small icon*/
.setContentTitle("通知")/*设置title*/
.setContentText("有新的通知")/*设置详细文本*/
.setWhen(System.currentTimeMillis())/*设置发出通知的时间为发出通知时的系统时间*/
.setTicker("有新的通知")/*设置发出通知时在status bar进行提醒*/
.setOngoing(true)/*setOngoing(boolean)设为true,notification将无法通过左右滑动的方式清除 * 可用于添加常驻通知,必须调用cancle方法来清除 */
.setAutoCancel(true)/*设置点击后通知消失*/
.setNumber(2)/*设置通知数量的显示类似于QQ那种,用于通知的合并*/
.setContentIntent(pendingIntent);/*点击跳转到MainActivity*/
mManager.notify(0, notifyBuilder.build());
时间: 2024-12-09 07:06:53