2016-01-05 93 views
1

我正在尝试制作一个按钮,它将更改Android棒棒糖中的中断过​​滤器(无,优先级,全部)。当我按下按钮时,日志显示Notification listener service not yet bound.。服务开始了,所以我想我试图把它绑定错了?我得到了"NLS Started"日志,但不是"NLS Bound"。这里是我的代码:Android服务尚未绑定

MainActivity.java

public class MainActivity extends Activity { 
    private NotificationService notifs; 
    private ServiceConnection connection; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     notifs = new NotificationService(); 
     connection = new ServiceConnection() { 
      @Override 
      public void onServiceConnected(ComponentName name, IBinder service) { 
       Log.d("NOTIF", "NLS Started"); 
       NotificationService.ServiceBinder binder = (NotificationService.ServiceBinder)service; 
       notifs = binder.getService(); 
      } 

      @Override 
      public void onServiceDisconnected(ComponentName name) { 
       Log.d("NOTIF", "NLS Stopped"); 
      } 
     }; 
     Intent intent = new Intent(this, NotificationService.class); 
     startService(intent); 
     bindService(intent, connection, Context.BIND_AUTO_CREATE); 
     if(notifs.isBound()) { 
      Log.d("NOTIFS", "NLS Bound"); 
     } 

     final Button b = (Button) findViewById(R.id.b); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (notifs.getCurrentInterruptionFilter() == NotificationService.INTERRUPTION_FILTER_NONE) { 
        //set all 
        b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_volume)); 
        notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_ALL); 
       } else if (notifs.getCurrentInterruptionFilter() == NotificationListenerService.INTERRUPTION_FILTER_PRIORITY) { 
        //set none 
        b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_off)); 
        notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_NONE); 
       } else { 
        //set priority 
        b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_priority)); 
        notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_PRIORITY); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     unbindService(connection); 
     connection = null; 
    } 
} 

NotificationService.java

public class NotificationService extends NotificationListenerService { 
    private final IBinder binder = new ServiceBinder(); 
    private boolean isBound = false; 

    public NotificationService() { 
    } 

    public class ServiceBinder extends Binder { 
     NotificationService getService() { 
      return NotificationService.this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     isBound = true; 
     return binder; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startid) { 
     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d("NOTIF", "Started"); 
     Toast.makeText(NotificationService.this, "NLS Started", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    public boolean isBound() { 
     return isBound; 
    } 
} 

回答

1

服务连接工作与bindService()唯一方法。因此,如果您收到“NLS Started”,表示服务连接的onServiceConnected()方法中的代码已执行,表明您的服务已成功绑定到您的相应活动。

就“NLS绑定”而言,有时服务连接需要花费一两秒钟的时间来将服务与活动绑定。这个延迟不会阻止你的代码的其余部分工作,即在服务绑定到你的活动之前,bindService()方法下的if语句将被执行。出于这个原因,我们使用服务连接。服务连接在创建时接收服务对象,并且还会通知服务是否被销毁。请参阅以下链接了解更多详情: -

http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent,android.content.ServiceConnection,int)