package com.example.notificationactivity;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
addNotification();
}
});
}
/**
* 添加一个notification
*/
private void addNotification(){
//得到NotificationManager对象
NotificationManager notificationManager = (NotificationManager)this.
getSystemService(Context.NOTIFICATION_SERVICE);
//创建一个Notification实例
Notification notification = new Notification(R.drawable.check, "催费通知", 2);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_ONE_SHOT);
//Notification设置最新事件信息
notification.setLatestEventInfo(this, "你好,提醒缴费", "你已欠费100元", pendingIntent);
//启动提醒
notificationManager.notify(0, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Android学习:Notification的使用
最新推荐文章于 2022-03-06 06:30:00 发布