使用startForeground();
*在serVice的onCreat()方法中调用startForeground()方法使当前进程成为前台进程
Notification notification = new Notification();
//通知栏没有展开时的显示内容
notification.icon = R.mipmap.ic_launcher;
notification.tickerText = "我的手机卫士时刻保护您";
//下拉通知栏的显示内容
notification.contentView = new RemoteViews(getPackageName() , R.layout.notifition_view);
//点击通知栏跳转到相应的应用里面
Intent intent = new Intent(this , MainActivity.class);
//这一句加不加没什么影响
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notification.contentIntent = PendingIntent.getActivity(this , 1 , intent , 0);
//这里的id不能是0
startForeground(1 , notification);
*在登录成功后调用一下代码,开启服务
startService(new Intent(this , NotKillSeivice.class));