发送通知:Notification

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

操做的意图。android

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

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

  Intent表示当即执行;ide

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

 

 

发送通知:Notification

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

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

 



 

 

 

在main.xml中:事件

 

<LinearLayoutget

    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);

    }

}

相关文章
相关标签/搜索