Android_Notification通知栏通知的实现

实现效果如图所示:




MainActivity.java文件
package com.yongninggo.helloworld;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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 send;
    private Button cancle;

    NotificationManager manager;
    int notification_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        send = (Button) findViewById(R.id.send);
        cancle = (Button) findViewById(R.id.cancle);

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setSmallIcon(R.drawable.image1);
                builder.setTicker("World");
                builder.setWhen(System.currentTimeMillis());
                builder.setContentTitle("标题栏");
                builder.setContentText("这个是显示出来的内容部分");

                Intent intent = new Intent(MainActivity.this, Activity.class);
                PendingIntent ma = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                builder.setContentIntent(ma);//设置点击过后跳转的activity

                /*builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
                builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
                builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动*/
                builder.setDefaults(Notification.DEFAULT_ALL);//设置全部

                Notification notification = builder.build();//4.1以上用.build();
                notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知的时候cancel掉
                manager.notify(notification_id,notification);
            }

        });

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.cancel(notification_id);
            }
        });
    }
}

activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yongninggo.helloworld.MainActivity"
    android:gravity="center_horizontal"
    android:layout_marginTop="10dip">

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送通知"/>

    <Button
        android:id="@+id/cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消通知"
        android:layout_marginLeft="10dip"/>
</LinearLayout>

提示音,闪光灯,震动效果需要添加权限:
<uses-permission android:name="android.permission.VIBRATE"> </uses-permission>



图标不知道什么原因,尝试了各种方法 反正就是个机器人,我的是红米3 MIUI Android5.1;(莫名的心酸呐~)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值