IntentService内部的线程停止更新通知

时间:2016-11-11 10:35:31

标签: android multithreading android-notifications android-intentservice java-threads

我有意向服务与地理围栏相关联。 service被称为通知发送和内部服务,它们是一个在10秒后更新通知的线程。

这是代码:

 @Override
protected void onHandleIntent(Intent intent) {
sendNotification("enter",2);

t=new Thread(new Runnable() { @Override public void run() { try {

                                    Thread.sleep(10000);


                                    sendNotification("PICK",2);



                                }
                                catch (Exception e)
                                {
                                    Log.e("ERROR",e.toString());
                                }
                            }
                        });
                        t.start();

}

这里是发送通知代码

private void sendNotification(String notificationDetails,String itineraryID) { // Create an explicit content Intent that starts the main Activity. Intent notificationIntent = new Intent(getApplicationContext(), Detail.class); notificationIntent.putExtra("ID",ItineraryID);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(Detail.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.drawable.to_chegando_icon)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.drawable.to_chegando_icon))
            .setColor(Color.RED)
            .setContentTitle("Saida da Escola"+ itineraryID)
            .setContentText(notificationDetails)
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    int n=Integer.parseInt(itineraryID);
    mNotificationManager.notify(n, builder.build());
}`

这项工作很好创建通知并在10秒后更新。

问题是当我创建通知然后从后台终止应用程序时(按主页按钮然后从后台应用程序中删除应用程序),此线程不会更新通知。这是为什么 ???任何解决方案

0 个答案:

没有答案