2019独角兽企业重金招聘Python工程师标准>>>

在android3.0之后,Android的通知与之前的版本有所区别。为了更好的使用,androidSDK引入了Android.app.Notificition.Builder这个类。这样就与之前使用发生了变化。那就说一下在Android3.0之后Notificaition的使用 import android.annotation.SuppressLint;
import android.app.Activity;import android.app.Notification;
import android.app.Notification.Builder;立即发表
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Intent; import android.content.res.Resources;
 import android.graphics.BitmapFactory;
import android.os.Bundle; import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
@SuppressLint("NewApi")
 public class MainActivity extends Activity
{
           private Button but1= null;
           private NotificationManager nm =null;
           private PendingIntent contentIntent = null;
       @Override
      public void onCreate(Bundle savedInstanceState)
      {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                init();
             }
       public void init(){
           but1 =(Button)super.findViewById(R.id.but1);
           but1.setOnClickListener(onclick);
           nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
           Intent notificationIntent = new Intent(this,MainActivity.class);
          contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
       }
          OnClickListener onclick = new OnClickListener(){
              private final int NOTIFICATION_BASE_NUMBER=110;
              private Builder builder = null;
              private Notification n = null;
              public void onClick(View arg0) {
            switch(arg0.getId()){
            case R.id.but1: NotificationManager nm = (NotificationManager) MainActivity.this                             .getSystemService(NOTIFICATION_SERVICE);
            Resources res = MainActivity.this.getResources();
        builder = new Notification.Builder(MainActivity.this);
        builder.setContentIntent(contentIntent) .
       setSmallIcon(R.drawable.ic_launcher)//设置状态栏里面的图标(小图标)                     .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表里面的图标(大图标)        .setTicker("this is bitch!") //设置状态栏的显示的信息
           .setWhen(System.currentTimeMillis())//设置时间发生时间
           .setAutoCancel(true)//设置可以清除
                      .setContentTitle("This is ContentTitle")//设置下拉列表里的标题
                      .setContentText("this is ContentText");//设置上下文内容
                    n = builder.getNotification();//获取一个Notification
                    n.defaults =Notification.DEFAULT_SOUND;//设置为默认的声音
                    nm.notify(NOTIFICATION_BASE_NUMBER, n);//显示通知 break; }
                               }
                      };
 }
 Layout.xml文件我就不发出来了,就一个Button。 当按下主界面的Button后会出现会在下拉列表中出现 This is ContentTitle。

转载说明出处.

转载于:https://my.oschina.net/arthurdemo/blog/70960

更多相关文章

  1. Android(安卓)图标外发光
  2. Android中TimePickerDialog的使用
  3. Android(安卓)怎么把GMT+8.0转化为UTC时间
  4. Android的闹钟
  5. Android(安卓)APP安装后不在桌面显示图标的应用场景举例和实现方
  6. actionbar 详解教程
  7. android listview滑动设置浮标半透明效果
  8. 【Android(安卓)开发教程】AnalogClock和DigitalClock
  9. android 应用图标大小

随机推荐

  1. Windows Phone 7 不温不火学习之《ListBo
  2. Android(安卓)画图之Matrix(二)
  3. Android(安卓)源代码目录结构1 - bionic
  4. android “分享”功能的实现
  5. Android的文件系统
  6. Android(安卓)NDK1、Android(安卓)NDK带
  7. android中的http框架,使其更加简单易用
  8. 【Android】TextView 属性详解
  9. Android(安卓)init.rc详解
  10. 使用Kotlin开发Android基础介绍