通知栏,这个很好理解吧,都懂就不扯了。
主要class:NotificationCompat(通知栏),然后在给它设置参数。根据你的需要来设置,再通知管理器 NotificationManager发送通知。一个简单的通知栏就完成了。它自带左右拖动可以清除。
直接上代码:
public class RingActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
//显示通知栏
NotificationCompat.Builder notificationCompat=new NotificationCompat.Builder(this);
//设置参数
notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);
notificationCompat.setContentTitle("闹钟");
notificationCompat.setContentText("一大波僵尸正在来袭");
notificationCompat.setSmallIcon(android.R.drawable.ic_lock_idle_alarm);
Intent intent=new Intent(RingActivity.this,RingActivity.class);
pendingIntent = PendingIntent.getBroadcast(RingActivity.this,0x101,intent,0);
//notificationCompat.setDeleteIntent(pendingIntent);
notificationCompat.setContentIntent(pendingIntent);
Notification notification=notificationCompat.build();
//通知管理器
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(0x101,notification);
}
}