Android之消息通知栏

 

一:先来效果图、

二:实现步骤、

1.xml布局实现、

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.feicui.notification.MainActivity">

    <Button
        android:id="@+id/btn_create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送通知"
        android:textSize="25sp" />

</LinearLayout>

2.activity的实现、

package edu.feicui.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RemoteViews;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    /**
     * 通知栏Notification
     */
    private NotificationManager mManager;
    private Notification mNotification;
    private PendingIntent mIntent;
    private String cll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cll = "今年27号过年哟!";
        ButterKnife.bind(this);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        init();
    }

    private void init() {
        //初始化通知栏管理者
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //意图数组
        Intent[] intents = {new Intent(this, NotificationAcitivity.class)};
        //待处理意图对象
        mIntent = PendingIntent.getActivities(this, 0, intents, 0);
        //消息栏通知对象
        mNotification = new Notification();
    }

    @OnClick(R.id.btn_create)
    public void create() {
        //设置在通知栏的消息图标
        mNotification.icon = R.mipmap.logo_new;
        //设置在通知栏的信息内容
        mNotification.tickerText = "重大消息";
        //设置默认的声音,此外还可以设置震动(需加入权限)
        mNotification.defaults = Notification.DEFAULT_SOUND;
        //添加灯光
//        mNotification.defaults=Notification.DEFAULT_LIGHTS;
        //不能删除
        mNotification.flags = Notification.FLAG_NO_CLEAR;
        //设置下拉时的显示布局
        RemoteViews convertView = new RemoteViews(getPackageName(), R.layout.layout_content);
        convertView.setImageViewResource(R.id.img, R.mipmap.logo_new);
        convertView.setTextViewText(R.id.txt, cll);
        mNotification.contentView = convertView;
        mNotification.contentIntent = mIntent;
        //发送通知
        // 第一个参数唯一的标识该Notification,第二个参数就是Notification对象
        mManager.notify(1, mNotification);
    }
}

 

3.AndroidManifest添加权限、

<uses-permission android:name="android.permission.VIBRATE"/>

 

 

4.跳转界面的xml、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="20dp"
        android:text="今年27号过年哟!" />

</LinearLayout>
 

5.跳转activity的实现、

package edu.feicui.notification;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Created by Administrator on 2017-1-20.
 */
public class NotificationAcitivity extends Activity {
    private NotificationManager mManager;
    private int index = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        //初始化通知栏管理者
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        index = 2;
        mManager.cancelAll();
    }
}

 

 

 

 

简单粗暴实用,你值得拥有、、、

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶已初秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值