2016-09-16 7 views
0

난 그냥 응용 프로그램은 내가 notifications.When 앱은 내가 통지를안드로이드 백그라운드 서비스가 실행하지만, 통지

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(); 
      } 
     } 

    } 

이 무엇입니까 개방되어지고 있지 않다 닫혀 app.When 작은 채팅을 구현하고를 받고 있지 않습니다 액티비티 코드입니다. 버튼의 클릭에 내가 서비스를 시작하고 있고 응용 프로그램이 opened.When 응용 프로그램입니다 때 작동하는 폐쇄 된 서비스가이

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

mmm ... 푸시 알림을 사용하지 않으시겠습니까? – Aspicas

+0

stopSelf() 또는 stopService (..)를 호출하지 않으시겠습니까 –

+0

서비스 코드 – Anjali

답변

0

확인 표시 통지의 코드

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); 
       } 
      }); 

를 실행하지 않는 것입니다 서비스가 끈적 거리는 서비스입니다

끈적한 서비스가 아니면 앱을 닫은 후에 활성화되지 않습니다

관련 문제