当前位置:操作系统 > 安卓/Android >>

发送通知:Notification

Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种
 
操作的意图。
 
  PendingIntent表示的是暂时执行的一种意图,是一种在产生某一事件之后才进行操作的Intent对象。
 
面试题:请解释Intent与PendingIntent的区别:
 
  Intent表示立即执行;
 
  PendingIntent表示暂缓执行,遇到特殊条件才执行。
 
 
 
 
 
发送通知:Notification
  Notification表示一种提示用户操作的组件。
 
  Notification表示的是一个通知,而NotificationManager表示的是一个发送通知的操作类。
 
 
 
 
 
 
 
 
 
 
在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);
 
    }
 
}
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,