Android服务不会在应用退出时发送通知

时间:2016-02-01 03:10:32

标签: android service notifications exit

我创建了后台服务,用于检查 Firebase数据库中的值更改,如果有任何更改,则发送Notification。只要应用程序正在运行,该服务就会通知值更改。当我关闭应用程序时,我停止接收通知。我的onStartCommand包含一个新的线程,包含一个firebase值事件监听器。随着价值的变化

我调用了sendNotification方法。这是我的完整服务类:

公共类NotifyService扩展了Service {

Firebase mRef;

@Override
public IBinder onBind(Intent p1)
{
    // TODO: Implement this method
    return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Toast.makeText(this,"service started",Toast.LENGTH_LONG).show();
    Firebase.setAndroidContext(this);
    mRef= new Firebase(myURL);
    Runnable run = new Runnable(){
        @Override
        public void run(){
            mRef.child("politics").child("story1").child("title").addValueEventListener(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot p1)
                    {
                        // TODO: Implement this method
                        String data = (String)p1.getValue();
                        sendNotification(data);
                    } 

                    @Override
                    public void onCancelled(FirebaseError p1)
                    {
                        // TODO: Implement this method
                    }
                });

            mRef.child("sports").child("story1").child("title").addValueEventListener(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot p1)
                    {
                        // TODO: Implement this method
                        String data = (String)p1.getValue();
                        sendNotification(data);
                    } 

                    @Override
                    public void onCancelled(FirebaseError p1)
                    {
                        // TODO: Implement this method
                    }
                });

            mRef.child("science").child("story1").child("title").addValueEventListener(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot p1)
                    {
                        // TODO: Implement this method
                        String data = (String)p1.getValue();
                        sendNotification(data);
                    } 

                    @Override
                    public void onCancelled(FirebaseError p1)
                    {
                        // TODO: Implement this method
                    }
                });

            mRef.child("gossip").child("story1").child("title").addValueEventListener(new ValueEventListener(){
                    @Override
                    public void onDataChange(DataSnapshot p1)
                    {
                        // TODO: Implement this method
                        String data = (String)p1.getValue();
                        sendNotification(data);
                    } 

                    @Override
                    public void onCancelled(FirebaseError p1)
                    {
                        // TODO: Implement this method
                    }
                });
        }};
    Thread serviceThread = new Thread(run);
    serviceThread.start();
    return Service.START_STICKY;
}
@Override
public void onDestroy() 
{
    Toast.makeText(this, "activity destroyed", Toast.LENGTH_LONG).show();
}

public void sendNotification(String title){
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotifyService.this);
    mBuilder.setSmallIcon(R.drawable.nicon); 
    mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.nlicon));
    mBuilder.setAutoCancel(true);
    mBuilder.setContentTitle("New Story")   
    .setContentText(title);
    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(NotifyService.this);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 
        0,           
        PendingIntent.FLAG_UPDATE_CURRENT      
    );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify((int)System.currentTimeMillis(), mBuilder.build());
}
}

我用

打电话
Intent i = new Intent(this, NotifyService.class);
startService(i);

什么是不让app退出后显示通知。我怀疑通知方法有问题。

1 个答案:

答案 0 :(得分:1)

我的服务显然是在设置中运行,但它实际上是在应用程序被杀后被杀死的。

让您的服务继续运行,而不是

startService(new Intent(this, MyService.class));

使用它:

Intent serviceintent =new Intent(this, MyService.class);
    PendingIntent pendingintent =PendingIntent.getService(this,0, serviceintent,0);
    AlarmManager alarm =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pendingintent);  
    alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5000, pendingintent);

这可确保您的服务每5秒钟继续使用AlarmManager,因此无论发生什么情况,服务都会继续运行。