展会信息港展会大全

android开发之Notification的NotificationManager类和Notification类
来源:互联网   发布日期:2015-10-03 10:40:41   浏览:1981次  

导读:Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notification。可以这样理解这两个 类,Notificati ...

Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。

设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notification。可以这样理解这两个 类,NotificationManager相当于顺丰快递小哥,notification代表的就是我们送的信件,我们要发送信件首先打电话给顺丰小 哥,相当于初始化NotificationManager,然后填写好信件,相当于初始化Notifiaction,然后小哥发送信件,就是 notificationmanager.notify(notifaction)

1)初始化NotificationManager: this.getSystemService(Context.NOTIFICATION_SERVICE);

2)初始化Notification:new Notification.Builder(this).builder();

main.xml:

<RelativeLayout 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"

tools:context=".Main" >

<Button

android:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="发送notification" />

<Button

android:id="@+id/deletebtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="200sp"

android:text="取消notification" />

</RelativeLayout>

Main.java:

package com.app.main;

import android.annotation.SuppressLint;

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.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class Main extends Activity {

NotificationManager manager = null;

Button button, btn1;

int responseCode = 0x123;

@SuppressLint("NewApi")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button = (Button) this.findViewById(R.id.btn);

btn1 = (Button) this.findViewById(R.id.deletebtn);

manager = (NotificationManager) this

.getSystemService(Context.NOTIFICATION_SERVICE);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

send();

}

});

btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

deleteMsg();

}

});

}

/**

*

*/

@SuppressLint("NewApi")

public void send() {

Intent intent = new Intent(Main.this, OtherActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

Notification notification = new Notification.Builder(this)

.setAutoCancel(true).setTicker("aaaa")

.setSmallIcon(R.drawable.ic_launcher).setContentTitle("bbbb")

.setContentText("ccccc")

.setDefaults(Notification.DEFAULT_VIBRATE).build();

manager.notify(responseCode, notification);

}

public void deleteMsg() {

manager.cancel(responseCode);

}

}

实现效果:

tips:在实例化Notification.Builder()实例的时候,必须setSmallIcon()或者setLargerIcon(),否 则notifaction不显示。

赞助本站

人工智能实验室

相关热词: NotificationManager android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港