package com.yaotai.standarview;
import com.yaotai.standarview.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class NotificationActivity extends Activity {
NotificationManager mNotificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
//1.设置NotificationManager实例
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button button = (Button) findViewById(R.id.buttonnotification1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
//2.拼装Notification实例
//这是上面的内容
Notification notificaty = new Notification(R.drawable.ic_launcher, "You have one email",System.currentTimeMillis() );
//下面是拼装形式
// Notification notification = new Notification();
// notification.icon=R.drawable.ic_launcher;
// notification.tickerText="启动其他";
// notification.when=System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 345,
new Intent(NotificationActivity.this,FormWidgetActivity.class), 0);
//这是展开的内容
//notification.setLatestEventInfo(NotificationActivity.this, "Your Email", "今天天气不错,要不要出去玩啊???", pendingIntent);
//可用下面代替
notificaty.contentIntent = pendingIntent;
notificaty.contentView=new RemoteViews(getPackageName(),R.layout.customnotification);
//3.触发提醒
mNotificationManager.notify(123, notificaty);
}
});
Button button2 = (Button) findViewById(R.id.buttonnotification2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
mNotificationManager.cancel(123);
}
});
}
@Override
protected void onStop() {
// TODO 自动生成的方法存根
mNotificationManager.cancel(123);
super.onStop();
}
}
notification使用步骤:
1.建立NotificationManager对象
2.建立Notification对象(下拉前的)
a.建立PendingIntent对象
b.调用setLatestEventInfo()方法,把PendingIntent加载(这是下拉后的)
3.触发提醒