该应用的界面如下,界面代码在此不再给出,源码github账户下载
MainActivity.java
public class MainActivity extends Activity {
private TextView tvTitle;
private TextView tvContent;
private Button btnSend;
private String title;
private String content; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tvTitle=(TextView) findViewById(R.id.editText1);
tvContent=(TextView) findViewById(R.id.EditText01);
btnSend=(Button) findViewById(R.id.btnSend); btnSend.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
send();
}
}); }
public void send(){
title=tvTitle.getText().toString();//标题
content=tvContent.getText().toString();//内容 //1.得到NotificationManager服务
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//2.实例化一个通知,指定图标、概要、时间
Notification n = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
//3.指定通知的标题、内容和intent
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
n.setLatestEventInfo(this, title, content, pi);
//指定声音
//n.defaults = Notification.DEFAULT_SOUND;
//4.发送通知
nm.notify(1, n);
} }
点击“发送”按钮,即可在通知栏显示发送的消息!