NotificationListenerService UI更新

时间:2014-04-28 14:35:07

标签: java android

使用以下方法我可以显示吐司。
但是我无法访问mHandler.post(new Runnable() { });块内的 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();                
        }
    });


}
}

2 个答案:

答案 0 :(得分:3)

声明你的变量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();                
        }
    });


}
}

答案 1 :(得分:1)

更改' sbn'的修饰符决赛:

public void onNotificationPosted(最终StatusBarNotification sbn){