Notification小案例

xml文件:

<Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:layout_height="wrap_content"

        android:text="显示通知" />

 

 

MainActivity类中:

        @SuppressLint("NewApi")

        public void click(View view){

            //1.获取到手机系统里面的通知管理器

            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            

            //2.实例化notification表示通知的具体内容

            Notification notification = new Notification(R.drawable.ic_launcher, "我是一个notification", System.currentTimeMillis());

           

            notification.flags = Notification.FLAG_INSISTENT;//永远存在

            Intent intent = new Intent();

            intent.setAction(Intent.ACTION_CALL);

            intent.setData(Uri.parse("tel:110"));

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            notification.setLatestEventInfo(this, "我是notification标题", "我是notification的文本", pendingIntent);

           

           

            //新的API

            /*Notification.Builder builder = new Builder(this);

            builder.setContentTitle("我是notification的标题")

            .setContentText("我是notification的内容")

            .setSmallIcon(R.drawable.ic_launcher)

            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

            Notification notification = builder.build();*/

            nm.notify(0, notification);

           

        }

 

清单文件中:

<uses-permission android:name="android.permission.CALL_PHONE"/>

 

posted @ 2015-11-04 23:36  飞牛冲天  阅读(166)  评论(0编辑  收藏  举报