public class NotificationTest extends Activity{    static final int NOTIFICATION_ID = 10;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button bn = (Button) findViewById(R.id.bn);        bn.setOnClickListener(new View.OnClickListener()        {            @Override            public void onClick(View source)            {                //创建一个启动其他Activity的Intent                Intent intent = new Intent(NotificationTest.this                    , OtherActivity.class);                PendingIntent pi = PendingIntent.getActivity(NotificationTest.this                    , 0, intent , 0);                //创建一个Notification                Notification notify = new Notification();                //为Notification设置图标,该图标显示在状态栏                notify.icon = R.drawable.notify;                //为Notification设置文本内容,该文本会显示在状态栏                notify.tickerText = "启动其他Activity的通知";                //为Notification设置发送时间                notify.when = System.currentTimeMillis();                //设置事件信息                notify.setLatestEventInfo(NotificationTest.this, "普通通知",                    "点击查看", pi);                //获取系统的NotificationManager服务                NotificationManager notificationManager = (NotificationManager)                    getSystemService(NOTIFICATION_SERVICE);                //发送通知                notificationManager.notify(NOTIFICATION_ID, notify);            }        });        Button del = (Button)findViewById(R.id.del);        del.setOnClickListener(new OnClickListener()        {            @Override            public void onClick(View v)            {                //获取系统的NotificationManager服务                NotificationManager notificationManager = (NotificationManager)                    getSystemService(NOTIFICATION_SERVICE);                //取消通知                notificationManager.cancel(NOTIFICATION_ID);            }        });    }}


运行结果:

更多相关文章

  1. android辅助圆按钮
  2. android studio gradle常用设置
  3. 使用ComponentName启动Activity
  4. android 取设备公网ip ,内网ip
  5. ViewPager无限轮播图
  6. notification android原生消息通知代码详解
  7. Android(安卓)设置DrawableRight和DrawableLeft 点击事件
  8. Android学习之键盘事件
  9. Android模拟强制下线通知功能实例代码

随机推荐

  1. Android开发学习笔记(五)Android五大布局
  2. Android AdbCommandRejectedException和c
  3. QT for Android HelloWorld.apk!耶
  4. Android中部署自己的su
  5. 【Android】gradle使用过程的问题解决汇
  6. Android Studio安装及常见错误
  7. Android(安卓)知识点积累(一)
  8. 获得G1的电池电量信息
  9. AndroidStudio、gradle、buildToolsVersi
  10. sqlite c库使用