读书人

发送通报:Notification

发布时间: 2013-10-08 16:46:23 作者: rapoo

发送通知:Notification

Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种

操作的意图。

PendingIntent表示的是暂时执行的一种意图,是一种在产生某一事件之后才进行操作的Intent对象。

面试题:请解释Intent与PendingIntent的区别:

Intent表示立即执行;

PendingIntent表示暂缓执行,遇到特殊条件才执行。

发送通知:Notification

Notification表示一种提示用户操作的组件。

Notification表示的是一个通知,而NotificationManager表示的是一个发送通知的操作类。

发送通报:Notification

在main.xml中:

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center_horizontal"

android:background="#000000">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

android:textColor="#ffffff"

android:text="请看上面的通知!"/>

</LinearLayout>

在MyNotificationDemo.java中:

package com.li.notification;

import android.os.Bundle;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.view.Menu;

import android.view.MenuItem;

import android.support.v4.app.NavUtils;

public class MyNotificationDemo extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

NotificationManager notificationManager =

(NotificationManager)super.getSystemService(Activity

.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.pic_m,"来自李叶文的消息",

System.currentTimeMillis()); //立刻发送一个消息

PendingIntent contentIntent = PendingIntent.getActivity(

this, 0, super.getIntent(), PendingIntent

.FLAG_CANCEL_CURRENT); //创建了一个PendingIntent对象

notification.setLatestEventInfo(this, "广西", "北海", contentIntent);

notificationManager.notify("LYW",R.drawable.pic_m,notification);

}

}

读书人网 >移动开发

热点推荐