2016-09-16 28 views
0

我只是實現小型聊天app.When應用程序被關閉我沒有得到notifications.When的應用程序打開我得到通知Android的後臺服務正在運行,但沒有得到通知

protected void onHandleIntent(Intent intent) { 

     final ResultReceiver receiver = intent.getParcelableExtra("receiver"); 
     String url = intent.getStringExtra("url"); 
     Bundle bundle = new Bundle(); 
     for(;;) { 
      if (!TextUtils.isEmpty(url)) { 
       receiver.send(STATUS_RUNNING, Bundle.EMPTY); 

       try { 
        String results = downloadData(url); 
        if (null != results) { 
         bundle.putString("result", results); 
         receiver.send(STATUS_FINISHED, bundle); 
         displayNotification(results); 
        } 
       } catch (Exception e) { 
        bundle.putString(Intent.EXTRA_TEXT, e.toString()); 
        receiver.send(STATUS_ERROR, bundle); 
       } 
      } 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

這是活動代碼。在按鈕的點擊我開始服務,它是工作時的應用程序是opened.When應用程序被關閉的服務沒有運行

if (bt != null) { 
      bt.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Intent intent = new Intent(Intent.ACTION_SYNC, null, this, BgIntentService.class); 
        String abbc = Intent.ACTION_SYNC; 
        Context ctx = MainActivity.this; 
        Intent intent = new Intent(abbc, null, ctx, BgIntentService.class); 
        intent.putExtra("url", url); 
        intent.putExtra("receiver", new ResultReceiver(new Handler()) { 
         @Override 
         protected void onReceiveResult(int resultCode, Bundle resultData) { 
          switch (resultCode) { 
           case BgIntentService.STATUS_RUNNING: 
            setProgressBarIndeterminateVisibility(true); 
            break; 
           case BgIntentService.STATUS_FINISHED: 
            String abc = resultData.getString("result"); 
            // Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show(); 
            break; 
           case BgIntentService.STATUS_ERROR: 
            String error = resultData.getString(Intent.EXTRA_TEXT); 
            Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show(); 
            break; 
          } 
         } 
        }); 
        startService(intent); 
       } 
      }); 

這是顯示通知的代碼

protected void displayNotification(String msg) { 
     Log.i("Start", "notification"); 
     NotificationManager mNotificationManager; 
     int numMessages = 0; 

    /* Invoking the default notification service */ 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 

     mBuilder.setContentTitle("New Message"); 
     mBuilder.setContentText("You've received new message."); 
     mBuilder.setTicker("New Message Alert!"); 
     mBuilder.setSmallIcon(R.drawable.ic_launcher); 

    /* Increase notification number every time a new notification arrives */ 
     mBuilder.setNumber(++numMessages); 

    /* Add Big View Specific Configuration */ 
     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

     String[] events = new String[6]; 
     events[0] = msg; 


     // Sets a title for the Inbox style big view 
     inboxStyle.setBigContentTitle("Big Title Details:"); 

     // Moves events into the big view 
     for (int i=0; i < events.length; i++) { 
      inboxStyle.addLine(events[i]); 
     } 

     mBuilder.setStyle(inboxStyle); 

    /* Creates an explicit intent for an Activity in your app */ 
     Intent resultIntent = new Intent(this, NotificationView.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(NotificationView.class); 

    /* Adds the Intent that starts the Activity to the top of the stack */ 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 

     mBuilder.setContentIntent(resultPendingIntent); 
     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    /* notificationID allows you to update the notification later on. */ 
     mNotificationManager.notify(1, mBuilder.build()); 

    } 
+0

嗯...爲什麼不使用推送通知後,它不會被激活? – Aspicas

+0

希望您不要致電stopSelf()或停止服務(..) –

+0

分享您的服務代碼 – Anjali

回答

0

確保你的服務是一個棘手的服務

如果它不是一個棘手的服務關閉應用程序