Android学习日记(yzy):Notification的简单运用

最近,老大要我修改一个蓝牙通知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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值