Notification

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值