Android后台服务正在运行,但没有收到通知

时间:2016-09-16 09:55:15

标签: android android-notifications intentservice

我正在实施一个小型聊天应用程序。当应用程序关闭时,我没有收到通知。当应用程序打开时,我收到通知

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

    }

这是活动代码。点击按钮我开始服务,当应用程序打开时它正在工作。当应用程序关闭时服务没有运行

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

    }

1 个答案:

答案 0 :(得分:0)

确保您的服务是一项棘手的服务

如果它不是粘性服务,则在关闭应用后它将无法激活