android 的Notification 看起来简单,但是api老是改。用的时候确实很麻烦。这里写一个折叠的Demo。
布局文件:activity_main

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    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="elfstton.cheerchip.com.notifitydemo.MainActivity">    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Collapsed"        android:onClick="collapsedNotify" />RelativeLayout>

点击事件,注意api的判断。

  public void collapsedNotify(View view) {        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);        Notification.Builder builder=new Notification.Builder(this);        builder.setSmallIcon(R.mipmap.ic_launcher);        builder.setContentIntent(pendingIntent);        builder.setAutoCancel(true);        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));        //通过RemoteViews创建自定义视图        RemoteViews  remoteViews=new RemoteViews(getPackageName(),R.layout.mynotification);        remoteViews.setTextViewText(R.id.textView,"可折叠式");        Notification notification= null;        //大于16        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {            notification = builder.build();        }else{            //小于16            notification=builder.getNotification();        }        notification.contentView=remoteViews;        //通过RemoteViews创建自定义的Notification视图        RemoteViews expandedView=new RemoteViews(getPackageName(),R.layout.notification_expanded);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {            notification.bigContentView=expandedView;        }        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        nm.notify(0,notification);    }

源码下载: https://github.com/AndriodWorld/androidnotification

更多相关文章

  1. 创建Android(安卓)SD卡遇到的问题
  2. [置顶] Android(安卓)View系统学习文章汇总
  3. android启动画面
  4. Android深入理解Context–Context使用的误区
  5. android 环境搭建helloworld
  6. 【android】Android(安卓)SDK 配置
  7. Android创建和使用数据库详细指南
  8. Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewA
  9. Android(安卓)4 编程入门经典

随机推荐

  1. Android socket网络编程要注意
  2. Android中定时器Timer和TimerTask的启动,
  3. android 签名文件
  4. Android常用控件以及用法
  5. android中的分享功能
  6. 国内中文Android开发者社区和论坛
  7. 三,android编码规范 & 常用布局 & 常用控
  8. ARCVM:Chrome OS 中运行 Android 应用程序
  9. Android(安卓)65K问题之65K来源探究
  10. Android 应用的自动升级、更新模块的实现