构建Notification的Acitivity:
package com.person.radiogrouptest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AtyUsingNotification extends Activity {
private Button shownoti;
private NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_using_notification);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 用特定的ID来取消掉通知--〉点击通知跳转到该界面之后消除显示的通知的方法
nm.cancel(R.layout.aty_using_notification);
shownoti = (Button) findViewById(R.id.shownoti);
shownoti.setOnClickListener(new View.OnClickListener() {
// 用getIntent()函数是获取当前Activity的Intent
// 还可以传入其他Acitivity,但是需要指定名称
@Override
public void onClick(View v) {
// 创建一个消息,名称为Ticker Text,并且伴随显示时间,图标为amap_end;
Notification send = new Notification(R.drawable.amap_end,
"Ticker Text", System.currentTimeMillis());
// 设置TickerText消息中的:标题、内容
send.setLatestEventInfo(AtyUsingNotification.this, "标题", "内容",
PendingIntent.getActivity(AtyUsingNotification.this, 1,
getIntent(), 0));
// 发送通知进行显示
nm.notify(R.layout.aty_using_notification, send);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/shownoti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
</LinearLayout>