Notification(状态栏通知)详解

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


     private Context mContext;
    private NotificationManager mNManager;
    private Notification notify1;
    Bitmap LargeBitmap =null;

    private static final int NOTIFYID_1=1;

    private Button btn_show_normal;
    private Button btn_close_normal;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       mContext=getApplicationContext();

        //创建大图标
        LargeBitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.iv_bg_lc);
        mNManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        bindView();


    }

    private void bindView(){

        btn_show_normal=(Button) findViewById(R.id.btn_show_normal);
        btn_close_normal=(Button) findViewById(R.id.btn_close_normal);

        btn_show_normal.setOnClickListener(this);
        btn_close_normal.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

          switch (v.getId()){

              case R.id.btn_show_normal:

                  //定义一登上PendingIntent点击Notification后启动一个Activity
                  Intent it=new Intent(mContext,OtherActivity.class);
                  PendingIntent pit=PendingIntent.getActivity(mContext,0,it,0);

                  //设置图片,通知标题,发送时间,提示方式等属性
                  Notification.Builder mBuilder=new Notification.Builder(this);
                  mBuilder.setContentTitle("你好")
                          .setContentText("好好好好好好好")
                          .setSubText("--记住我叫刘德华")
                          .setTicker("收到刘德华发过来的信息")
                          .setWhen(System.currentTimeMillis())
                          .setSmallIcon(R.mipmap.ic_lol_icon)
                          .setLargeIcon(LargeBitmap)
                          .setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
                          .setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.biaobiao))
                          .setAutoCancel(true)
                          .setContentIntent(pit);

                  notify1=mBuilder.build();
                  mNManager.notify(NOTIFYID_1,notify1);

                  break;

              case R.id.btn_close_normal:

                  mNManager.cancel(NOTIFYID_1);
                  break;

          }



    }
}

猜你喜欢

转载自blog.csdn.net/weixin_37744986/article/details/80922983