public class ForegroundService extends Service { /** * id不可设置为0,否则不能设置为前台service */ private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001; private boolean isRemove=false;//是否需要移除 private int count; private boolean quit; private MyJPushReceiver mReceiver; private IntentFilter mIF; private Thread thread; int timemin = 0,waittime=1000; /** * 用于接收从客户端传递过来的数据 */ class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case 6: Log.i("map", "thanks,Service had receiver message from client!"); break; default: super.handleMessage(msg); } } } /** * 创建Messenger并传入Handler实例对象 */ final Messenger mMessenger = new Messenger(new IncomingHandler()); /** * Notification */ public void createNotification(){ //使用兼容版本 NotificationCompat.Builder builder=new NotificationCompat.Builder(this); //设置状态栏的通知图标 builder.setSmallIcon(R.mipmap.ic_launcher); //设置通知栏横条的图标 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ease_icon_marka)); //禁止用户点击删除按钮删除 builder.setAutoCancel(false); //禁止滑动删除 builder.setOngoing(true); //右上角的时间显示 builder.setShowWhen(true); //设置通知栏的标题内容 builder.setContentTitle("I am Foreground Service!!!"); //创建通知 Notification notification = builder.build(); //设置为前台服务 startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("lgqq","body=====接受到推送下来的消息=onStartCommand"+""); // int i=intent.getExtras().getInt("cmd"); // if(i==0){ // if(!isRemove) { // createNotification(); //// stopForeground(false); // } // isRemove=true; // }else { // //移除前台服务 // if (isRemove) { // stopForeground(true); // } // isRemove=false; // } flags =START_STICKY ; return super.onStartCommand(intent, flags, startId); } @Override public void onStart(final Intent intent, int startId) { super.onStart(intent, startId); ShareUtil.sharedPint("ifnotifica",1); timemin=0; waittime = 1000; Log.i("lgqq","body=====接受到推送下来的消息=onStart"+""); thread = new Thread(new Runnable() { @Override public void run() { // 每间隔一秒count加1 ,直到quit为true。 while (!quit) { try { Thread.sleep(waittime); Log.i("lgqq","body=====接受到推送下来的消息=6666666666666"+""); System.out.println("map============="); RemoteViews customView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.kongreveiver); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()); int ifnotifica = ShareUtil.getSharedInt("ifnotifica"); if (ifnotifica==0){ thread.join(); } if (timemin==0){ mBuilder .setDefaults(Notification.DEFAULT_ALL); waittime = 60000; customView.setTextViewText(R.id.timemy,"刚刚"); }else { customView.setTextViewText(R.id.timemy,timemin+"分钟前"); } customView.setTextViewText(R.id.name,intent.getExtras().getString("name")); Intent intentCancel = new Intent(getApplicationContext(),NotificationBroadcastReceiver.class); PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(getApplicationContext(),0, intentCancel,PendingIntent.FLAG_ONE_SHOT); timemin ++; Intent companyIntroduce = new Intent(getApplicationContext(), EaseBaiduMapActivity.class); // companyIntroduce.putExtra("name", name); int notifyId = (int) System.currentTimeMillis(); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); mBuilder//设置通知栏标题 // .setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE)) //设置通知栏显示内容 .setContent(customView) .setContentIntent(pendingIntent) //设置通知栏点击意图 .setDeleteIntent(pendingIntentCancel)//取消消息回调 // .setTicker(context.getPackageName() + "消息")//通知首次出现在通知栏,带上升动画效果的 .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级 .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消 //.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) // .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合 // .setDefaults(Notification.DEFAULT_SOUND) //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission .setSmallIcon(R.mipmap.ic_launcher); // mNotificationManager.notify(notifyId, notify); mNotificationManager.notify(0, mBuilder.build()); } catch (InterruptedException e) { e.printStackTrace(); } count++; } } }); thread.start(); } @Override public void onDestroy() { //移除前台服务 if (isRemove) { stopForeground(false); } isRemove=false; super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return mMessenger.getBinder(); } @Override public void onCreate() { super.onCreate(); Log.i("lgqq","body=====接受到推送下来的消息=onCreate"+""); mReceiver = new MyJPushReceiver() { @Override public void onReceive(Context context, Intent intent) { Intent a = new Intent(ForegroundService.this, ForegroundService.class); startService(a); Intent companyIntroduce = new Intent(context, MyServiceTestActivity.class); // companyIntroduce.putExtra("name", name); int notifyId = (int) System.currentTimeMillis(); PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); mBuilder .setContentText("name") //设置通知栏显示内容setContent、setContentText二选一 // .setContent(customView) .setContentIntent(pendingIntent) //设置通知栏点击意图 // .setNumber(mynum) //设置通知集合的数量 .setTicker(context.getPackageName() + "测试消息")//通知首次出现在通知栏,带上升动画效果的setTicker、setTextViewText二选一 .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间。使用setContent,只能到customView显示 .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级 .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消 .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单DEFAULT_ALL是声音与震动,DEFAULT_SOUND声音 .setSmallIcon(R.mipmap.ic_launcher); // mNotificationManager.notify(notifyId, mBuilder.build());//发出通知 } }; mIF = new IntentFilter(); //自定义action mIF.addAction("com.restart.service"); //注册广播接者 registerReceiver(mReceiver, mIF); // Log.i(TAG, "Service is invoke Created"); } }
收到通知的时候调用
private Intent mForegroundService;
mForegroundService=new Intent(context, ForegroundService.class);
mForegroundService.putExtra("name",bundle.getString(JPushInterface.EXTRA_MESSAGE)); context.startService(mForegroundService);