最近,老大要我修改一个蓝牙通知apk的软件异常,即在android6.0的情况下apk会报出sorry!apk exception,will
exit 的异常,后来发现,这个版本中Notiication移除了最初的方法Notification.setLatestEventInfo(),我们在
SDK23版本之后只能使用Notification.Builder来获取实例。再就是运行时的权限问题,蓝牙的扫描需要在Activity动
态的添加LOCATION的权限。具体的API功能差异可查看http://blog.csdn.net/t000818/article/details/52218574。
和http://blog.csdn.net/tangxl2008008/article/details/51334604。
因此,简单的写一下,在使用build的情况下的消息通知推送功能代码:
消息功能最重要的两个类为:类NotificationManager 和 类Notification ,Notification 负责将要执行的
功能和参数,再通过NotificationManager 推送出来,类PendingIntent则实现再点击通知框时的跳转功能。代码如下:
public class MainActivity extends Activity { final static private String TAG = "MainActivity"; private NotificationManager notificationManager = null; private PendingIntent contentIntent = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); } public void onClick(View view){ Log.e(TAG,"click successfully "); setNotification(); } private void setNotification(){ Intent intent = new Intent(com.yzy.btnoticationdemo.MainActivity.this,com.yzy.btnoticationdemo.SecondActivity.class); contentIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0); Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.app_info); Notification notification = new Notification.Builder(this) .setContentTitle("New mail from Notification") .setContentText("this is the detail content from Notification") .setSmallIcon(R.mipmap.app_info) .setLargeIcon(mBitmap) .setContentIntent(contentIntent) .build(); notificationManager.notify(0, notification); } }