Notification

来源:互联网 发布:10221淘宝 编辑:程序博客网 时间:2024/05/14 10:20

    • Notification实例演示
      • MainActivity
      • activity_mainxml
      • 运行图

notification就是通知,在这里是指状态栏中弹出的提示消息。
这里写图片描述

Notification实例演示

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button mBtn1;    private Button mBtn2;    private NotificationManager mNotificationManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mBtn1= (Button) findViewById(R.id.button_1);        mBtn2= (Button) findViewById(R.id.button_2);        //初始化notificationManager        mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        mBtn1.setOnClickListener(this);        mBtn2.setOnClickListener(this);    }    @Override    public void onClick(View v) {switch (v.getId()){    case R.id.button_1:        createNotification();        break;    case R.id.button_2:        mNotificationManager.cancel(1);        Intent intent=new Intent(getApplicationContext(),MainActivity.class);//设置pendingIntent事件        //设置PendingIntent的使用方法        PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),2,intent,PendingIntent.FLAG_ONE_SHOT);        Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).                setTicker("我是一个消息").setContentTitle("我是一个标题").setContentText("我是文本").                setContentInfo("我是内容").setContentIntent(pend).setAutoCancel(true).setWhen(System.currentTimeMillis()).build();        mNotificationManager.notify(2,notification);//通知管理器将通知添加        break;}    }    private void createNotification() {        Notification notification=new Notification();//初始化notification        notification.icon= R.mipmap.ic_launcher;//设置通知的图片        notification.tickerText="卖报卖报啦";//设置状态栏文本        notification.flags=Notification.FLAG_AUTO_CANCEL;//设置为可以取消        Intent intent=new Intent(getApplicationContext(),MainActivity.class);//设置pendingIntent事件        //设置PendingIntent的使用方法        PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);        notification.setLatestEventInfo(getApplicationContext(),"阅兵什么时候开始","9月3号上午10点,天安门广场",pend);        notification.when=System.currentTimeMillis();        mNotificationManager.notify(1,notification);//通知管理器将通知添加    }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"><Button    android:id="@+id/button_1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="发出通知"/>    <Button        android:id="@+id/button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="取消通知/发出通知"/></LinearLayout>

运行图

这里写图片描述

这幅图是用一个按钮发出通知,用另一个按钮取消通知。

这里写图片描述

这幅图是分别用两个按钮各发出一个通知,没有取消按钮,当第二个按钮按下的时候通知栏就会有两个通知同时存在

这里写图片描述

这幅图第二个按钮既有取消第一个通知的功能,也有发出第二个通知的功能

0 0
原创粉丝点击