我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。

效果图

介绍了部分功能具体请参照API

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.notificationmananger.MainActivity" >    <LinearLayout  android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" >        <Button  android:id="@+id/btn_send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送通知" />        <Button  android:id="@+id/btn_cancle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消通知" />    </LinearLayout></RelativeLayout>

MainActivity.java

package com.example.notificationmananger;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Notification.Builder;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;@SuppressLint("NewApi")public class MainActivity extends Activity implements OnClickListener {    private NotificationManager nManager;// 通知控制类    private int notifity_ID;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        findViewById(R.id.btn_send).setOnClickListener(this);        findViewById(R.id.btn_cancle).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.btn_send:            sendNotification();            break;        case R.id.btn_cancle:            nManager.cancel(notifity_ID);            break;        default:            break;        }    }    /** * 构造notification并发送到通知栏 Notification 的builder */    private void sendNotification() {        Intent intent = new Intent(this, MainActivity.class);        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);        Builder builder = new Notification.Builder(this);        builder.setSmallIcon(R.drawable.ic_launcher);// set up icon        builder.setTicker("Hello");// phone state hint        builder.setWhen(System.currentTimeMillis());// set up time        builder.setContentTitle("通知栏通知");// set up title        builder.setContentText("我来之notificationdome");// set up notification // content        builder.setContentIntent(pIntent); // set up 意图// builder.setDefaults(Notification.DEFAULT_SOUND); // set up suond// builder.setDefaults(Notification.DEFAULT_LIGHTS); // set up light 灯// builder.setDefaults(Notification.DEFAULT_VIBRATE);// set up vibrate 震动        builder.setDefaults(Notification.DEFAULT_ALL);// set up all 三种效果都有         Notification notification = builder.build();        notification.flags |= Notification.FLAG_AUTO_CANCEL;//onclick notification is cancel         notification.flags |=Notification.FLAG_NO_CLEAR;//notification is no clearn         nManager.notify(notifity_ID, notification);    }}

AndroidManifest.xml 添加权限

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

更多相关文章

  1. android 在listView中如果需要使用复选框功能,强烈建议使用图片作
  2. Android(安卓)版本区别
  3. Vuforia的Samples详细解析(Android)
  4. Android各个版本的区别
  5. 关于直接在View中实现Gesture的功能
  6. Android(安卓)快速开发框架:推荐10个框架
  7. Manifest.permission 这个类定义了android全部的权限共106个
  8. Android实现Service下载文件,Notification显示下载进度的示例
  9. Android(安卓)Studio 超级全常用快捷

随机推荐

  1. Android(安卓)wifi的WifiInfo对象详解
  2. Android仿QQ圆形头像
  3. android manifest.xml中元素含义
  4. android httpclient 上传文件
  5. Android(安卓)P Launcher APP替换图标不
  6. Android蓝牙耳机接听挂断电话流程
  7. android:windowSoftInputMode属性使用
  8. Android的NDK开发(1)————Android(安
  9. Android输入法扩展之外接键盘中文输入
  10. android中getSystemService详解