Android 编程下通知的发送
通知的发送一般是在手机接收到信息或是手机在后台的一项操作完成时用于提示用户的一条信息,通知的实现分为以下三步:
- 创建通知管理器(NotificationManager)
- 创建通知(Notification)并填充构成通知的数据
- 使用通知管理器发送通知
通知发送的效果图如下,第一张图的左上角的三角形为收到通知时的提示图标,第二张图为下拉通知进行查看时的效果:
package cn.sunzn.notify; import android.R.drawable; 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.Menu; import android.view.View; public class MainActivity extends Activity { private NotificationManager manager; private Notification notification; private Intent intent; private PendingIntent contentIntent; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public void sendnotify(View view) { notification = new Notification(drawable.stat_sys_warning, "通知", System.currentTimeMillis()); intent = new Intent(getApplicationContext(), MainActivity.class); contentIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0); notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知正文", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL; manager.notify(100, notification); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
专注移动互联网产品设计研发 分享最新的移动互联网产品和技术