如http://blog.csdn.net/yyingwei/article/details/8509402
运行一个service
@Override public void onDestroy() { // Make sure our notification is gone. stopForegroundCompat(R.string.system_message_title); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Builder builder = new Notification.Builder(this); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DeskSetting.class), 0); builder.setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_launcher)// 设置状态栏里面的图标(小图标) // .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表里面的图标(大图标) // .setTicker("this is bitch!") //设置状态栏的显示的信息 .setOngoing(false) .setOnlyAlertOnce(true) .setShowWhen(false) .setOnlyAlertOnce(true) .setPriority(Notification.PRIORITY_MIN) .setWhen(System.currentTimeMillis())// 设置时间发生时间 .setAutoCancel(true)// 设置可以清除 .setContentTitle(getText(R.string.fetion_launcher_started))// 设置下拉列表里的标题 .setContentText(getText(R.string.about_desk));// 设置上下文内容 Notification notification = builder.getNotification(); notification.flags = Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_NO_CLEAR; notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; Log.d(TAG, "notification.flags " + notification.flags); startForeground(3030, notification); // startForegroundCompat(1166, notification); mNM.cancel(3030); return super.onStartCommand(intent, flags, startId); }
PS:system应用的话,直接在AndroidManifest中的application标签加入android:persistent="true"就好