2016-01-06 27 views
0

我想做一個按鈕,將改變Android棒棒糖中的中斷過​​濾器(無,優先,全部)。當我按下按鈕時,日誌顯示W/NotificationListenerService[NotificationService]: Notification listener service not yet bound.並且不會更改中斷過濾器。我收到了"NLS Started""NLS Bound"日誌。爲什麼它給我這個警告?這裏是我的代碼:Android:服務「尚未綁定」,但onBind()被稱爲?

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

     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; 
     Log.d("NOTIF", "NLS Bound"); 
     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

您是否在聲明中聲明'NotificationService'與[documentation](http://developer.android.com/reference/android/service/notification/NotificationListenerService.html)中顯示的相同? –

+1

發佈您的清單 –

+0

感謝您的回覆,我已在清單中正確聲明瞭它是@qbix誰給了我正確的解決方案。 – yawers

回答

2

更換onBind()方法在你的服務,這樣的:

@Override 
public IBinder onBind(Intent intent) { 
    isBound = true; 
    String action = intent.getAction(); 
    Log.d("NOTIF", "onBind: " + action); 

    if (SERVICE_INTERFACE.equals(action)) { 
     Log.d("NOTIF", "Bound by system"); 
     return super.onBind(intent); 
    } else { 
     Log.d("NOTIF", "Bound by application"); 
     return binder; 
    } 
} 

此外,檢查您的服務如清單所示在清單中聲明documentation的概述。

如果您的服務在清單中正確聲明,並且在安全/聲音&通知中啓用了通知訪問,系統將使用操作NotificationListenerService.SERVICE_INTERFACE綁定到服務。對於此綁定請求,服務必須返回NotificationListenerService的綁定器,即super.onBind(intent)

+0

非常感謝!這讓我絆了一會兒,但讓你的修改解決了它! – yawers