Android Notification.Builder通知 Android Notification.Builder通知案例分享
y16179162 人气:0想了解Android Notification.Builder通知案例讲解的相关内容吗,y16179162在本文为您仔细讲解Android Notification.Builder通知的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Notification.Builder,通知,下面大家一起来学习吧。
随着Android系统的不断升级,相关Notification的用法有很多种,有的方法可能已经被android抛弃了,下面为大家分享一下个人如何实现Android Notification通知小案例源代码,供大家参考。
Android Notification.Builder通知小案例,具体代码示例如下:
package com.example.day6ke; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btn; private NotificationManager Manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn= (Button) findViewById(R.id.bnt); Manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); btn.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { Notification.Builder builder=new Notification.Builder(MainActivity.this); builder.setContentTitle("通知"); builder.setContentText("正文"); builder.setSmallIcon(R.mipmap.ic_launcher); //的到PendingIntent(延迟意图)的对象 PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 2020,new Intent(MainActivity.this, Main2Activity.class), PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pendingIntent); //点击通知是否消失 该方法与PendingIntent 一起使用才有效果 builder.setAutoCancel(true); Manager.notify(1,builder.build()); } }); } }
加载全部内容