Android 发送通知
实现代码如下,代码中有详细注释:
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.etTitle); tvContent=(TextView) findViewById(R.id.etContent); btnSend=(Button) findViewById(R.id.btnSend); btnSend.setOnClickListener(new OnClickListener(){public void onClick(View v) {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); }}