Android 通知(Notification)
阿新 • • 發佈:2019-03-11
eat src ima 介紹 後臺 cti pack ati 題目
1.介紹
2.常用屬性
3.java後臺代碼
package com.lucky.test30notification; import android.app.Notification; import android.app.NotificationManager; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; importandroid.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button1; Button button2; NotificationManager notificationManager; //定義一個通知管理器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=findViewById(R.id.button); button2=findViewById(R.id.button2); //實例化通知管理器 notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); button1.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Overridepublic void onClick(View v) { Notification.Builder builder=new Notification.Builder(MainActivity.this); builder.setSmallIcon(R.mipmap.img07); //設置圖標 builder.setTicker("新消息");//消息來時的提示內容 builder.setContentTitle("新通知");//設置通知題目 builder.setContentText("虎撲ios上線了");//設置通知內容 builder.setDefaults(Notification.DEFAULT_ALL);//設置默認格式 notificationManager.notify(0x01,builder.build()); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { notificationManager.cancel(0x01); //取消通知 } }); } }
Android 通知(Notification)