使用以下方法我可以显示 toast 。
但我无法从 mHandler.post(new Runnable() { });
block 内部访问 sbn 对象。
如何实现此功能?
public class NLService extends NotificationListenerService {
static Handler mHandler;
public void onCreate() {
super.onCreate();
mHandler = new Handler();
displayToast("Service Started NL");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}
}
最佳答案
将变量声明为final。
public class NLService extends NotificationListenerService {
static Handler mHandler;
public void onCreate() {
super.onCreate();
mHandler = new Handler();
displayToast("Service Started NL");
}
@Override
public void onNotificationPosted(final StatusBarNotification sbn) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}
}
关于java - 通知监听器服务 UI 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343931/