public class MainActivity extends Activity { private NotificationManager manager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Notification notification = new Notification(R.drawable.ic_launcher, "MyApp", System.currentTimeMillis()); Intent intent = new Intent(MainActivity.this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_NO_CLEAR; notification.setLatestEventInfo(MainActivity.this, "提示文字", "", pendingIntent); manager.notify(1, notification); } return super.onKeyDown(keyCode, event); } }
注意在onResume和onPause里对后台的服务进行处理
Notification的详细用法,参见http://blog.csdn.net/feng88724/article/details/6259071