您的位置:首页 > 移动开发 > Android开发

Notification通知的详解

2016-05-09 19:09 501 查看
Notification我不知道大家用的多不多,对于我来说这算是第一次详细的来接触它。

Notification是被用于通知现在的一条消息,它是一个持久性的消息,知道关闭它或者关闭设备后,它才不会保持显示,Android为我门提供了一个BuIilder嵌套类用来创建一个通知,我们可以通过传递一个Context来实例化Builder类,使用build()来创建一个Notification.

Notification.Builder含有很多方法这里就不一一说明,但是其中最主要的方法是addAction()和setContentIntent()方法,我们可以使用他们为通知添加一个动作,当我们触碰到通知的时候就可以发现,我们在此时就可以执行这个动作。(同时此处结合PendingItent类做此时的动作表示。)

*****PendingIntent 类是一个待处理的意图,这个动作常常是在将来的某个时刻调用的一个操作类,这里不排除是系统调用。(我们接下来的项目中将会使用此类用来描述Notification的简单使用。)

PendingIntent类中的动作是Context类中的几个方法之一。startActivity、startService或者是sendBroadcast.使用一个Intent 传递个下一个Context的startActivity方法相信大家都很了解,这里就不做解释了。

使用PendingIntent来启动一个活动的代码,具体如下:

Intent  it = new Intent();

PendingIntent pl = PendingIntent.getActivity(context,0,it,0);

pt.send(); (此时的send方法在用户触碰后进行调用)

PendingIntent方法中的所使用的其它静态实例方法还有startActivities(启动活动)、getService(开启服务)、getBroadcast(发送广播)。

******

要发布的通知可以使用NotificationManager,这是Android系统的内建服务。我们可以通过getSystemService来获取它。 之后使用notify()发布通知。发布通知的时候我们需要传递一个Id用来获取当前的唯一通知。如果想取消这个通知可以调用cancel方法来实现。

具体的操作代码如下:

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"

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=".MainActivity" >

<Button

android:id="@+id/btn_set"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="sure" />

<Button

android:id="@+id/btn_cel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="clear" />

</LinearLayout>

以上是我们的布局代码,为了方便起见,我们使用的是两个button进行操作。

Main.java

package com.example.notification;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

@SuppressLint("NewApi")

public class MainActivity extends Activity {

// 调用系统的NotificationManager 调用notify方法发布通知,需要获取的唯一id

private int id = 1001;

private Button btn_sure, btn_cel;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Notification notification = new Notification();

btn_sure = (Button) findViewById(R.id.btn_set);

btn_cel = (Button) findViewById(R.id.btn_cel);

btn_sure.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

setNotification(btn_sure);

}

});

btn_cel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

clearNotification(btn_cel);

}

});

}

private void setNotification(View view) {

Toast.makeText(this, "sure", Toast.LENGTH_LONG).show();

// TODO Auto-generated method stub

Intent intent = new Intent(this, Sead.class);

// 此处传递的Activty是利用点击消息便会进入下一界面,并且显示的消息消失

PendingIntent intentPendingIntent = PendingIntent.getActivity(this, 0,

intent, 0);

Notification notification = new Notification.Builder(this)

.setContentTitle("NOTIFICATION")

.setContentText("you notification")

.setSmallIcon(R.drawable.ic_launcher)

.setContentIntent(intentPendingIntent)

.setAutoCancel(true)

.addAction(android.R.drawable.ic_menu_gallery, "oPEN",

intentPendingIntent).build();

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manager.notify(id, notification);

Toast.makeText(this, notification + "", Toast.LENGTH_LONG).show();

notification.defaults |= Notification.DEFAULT_VIBRATE;

}

void clearNotification(View view) {

// 通过调用NotificationManager类实现取消通知

Toast.makeText(this, "cel", Toast.LENGTH_LONG).show();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.cancel(id);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

同时我们在点击操作通知的时候设置了Sead.xml页面,该页面也只是提供给我们操作的可执行行。以上是我这次学习的总结。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 实例