gpt4 book ai didi

java - Android服务尚未绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 01:40:06 24 4
gpt4 key购买 nike

我正在尝试制作一个按钮来更改 Android Lollipop 中的中断过​​滤器(无、优先级、全部)。当我按下按钮时,日志显示 Notification listener service not yet bound.。服务启动了,所以我想我尝试绑定(bind)错误?我收到了 "NLS Started" 日志,但没有收到 "NLS Bound" 日志。这是我的代码:

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

最佳答案

服务连接仅适用于 bindService() 方法。因此,如果您获得“NLS Started”,这意味着来自服务连接的 onServiceConnected() 方法的代码已被执行,这表明您的服务已成功与您各自的 Activity 绑定(bind)。

就“NLS Bound”而言,有时服务连接需要一两秒钟才能将服务与 Activity 绑定(bind)。这种延迟不会阻止您的其余代码工作,即 bindService() 方法下面的 if 语句甚至会在服务与您的 Activity 绑定(bind)之前执行。为此,我们使用服务连接。服务连接在创建时接收服务对象,如果服务被销毁也会收到通知。有关详细信息,请参阅以下链接:-

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

关于java - Android服务尚未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com