package activitys;
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.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class NotificationActivity extends Activity implements View.OnClickListener {
private static final int NOTE_ID = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Post New Notification");
button.setOnClickListener(this);
setContentView(button);
}
@Override
public void onClick(View v) {
handler.postDelayed(task, 10000);
Toast.makeText(this, "Notification will post in 10 seconds",
Toast.LENGTH_SHORT).show();
}
private Handler handler = new Handler();
private Runnable task = new Runnable() {
@Override
public void run() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent launchIntent = new Intent(getApplicationContext(),
Test.class);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, launchIntent, 0);
Notification note = new Notification(R.drawable.icon,
"Something Happened", System.currentTimeMillis());
note.setLatestEventInfo(getApplicationContext(), "Finished!",
"Click Here!", contentIntent);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTE_ID, note);
}
};
}
//新建状态栏通知 baseNF=newNotification(); //设置通知在状态栏显示的图标 baseNF.icon=R.drawable.icon; //通知时在状态栏显示的内容 baseNF.tickerText="YouclickedBaseNF!"; //通知的默认参数DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS. //如果要全部采用默认值,用DEFAULT_ALL. //此处采用默认声音 baseNF.defaults=Notification.DEFAULT_SOUND; //第二个参数:下拉状态栏时显示的消息标题expandedmessagetitle //第三个参数:下拉状态栏时显示的消息内容expandedmessagetext //第四个参数:点击该通知时执行页面跳转 baseNF.setLatestEventInfo(Lesson_10.this,"Title01","Content01",pd); //发出状态栏通知 //ThefirstparameteristheuniqueIDfortheNotification //andthesecondistheNotificationobject. nm.notify(Notification_ID_BASE,baseNF);
baseNF.defaults=Notification.DEFAULT_SOUND;
notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3"); notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"6"); notification.flags|=notification.FLAG_INSISTENT;
notification.defaults|=Notification.DEFAULT_VIBRATE; long[]vibrate={0,100,200,300}; notification.vibrate=vibrate;Long型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的时间,第三个参数是第二次振动的时间,以此类推,随便定义多长的数组。但是采用这种方法,没有办法做到重复振动。
notification.defaults|=Notification.DEFAULT_LIGHTS; notification.ledARGB=0xff00ff00; notification.ledOnMS=300; notification.ledOffMS=1000; notification.flags|=Notification.FLAG_SHOW_LIGHTS;其中ledARGB表示灯光颜色、ledOnMS亮持续时间、ledOffMS暗的时间。
flags: Notification.FLAG_INSISTENT;//让声音、振动无限循环,直到用户响应 Notification.FLAG_AUTO_CANCEL;//通知被点击后,自动消失 Notification.FLAG_NO_CLEAR;//点击‘Clear‘时,不清楚该通知(QQ的通知无法清除,就是用的这个 //自定义下拉视图,比如下载软件时,显示的进度条。 Notificationnotification=newNotification(); notification.icon=R.drawable.icon; notification.tickerText="Custom!"; RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom); contentView.setImageViewResource(R.id.image,R.drawable.icon); contentView.setTextViewText(R.id.text,"Hello,thismessageisinacustomexpandedview"); notification.contentView=contentView; //使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法 //但是必须定义contentIntent notification.contentIntent=pd; nm.notify(3,notification);
要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段。以下示例代码是完整步骤:
1、创建一个自定义的消息布局 view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000" /> </LinearLayout>
//2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image,R.drawable.icon); contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”); notification.contentView = contentView; //3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent;
//4、发送通知
mNotificationManager.notify(2,notification); //以下是全部示例代码 //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns); //定义Notification的各种属性 int icon = R.drawable.icon; //通知图标 CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示 long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示 //用上面的属性初始化Nofification Notification notification = new Notification(icon,tickerText,when); RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image, R.drawable.iconempty); contentView.setTextViewText(R.id.text, "Hello,this is JC"); notification.contentView = contentView; Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent; //把Notification传递给NotificationManager mNotificationManager.notify(0,notification);
Android技术精髓-Notification Activity
原文:http://blog.csdn.net/zhangtengyuan23/article/details/18550927