2016-01-05 97 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)