我正在使用Wrox Professional Android 2应用程序开发一书在Android上学习蓝牙编程。发现监视器示例(第432页)包含以下代码片段:
BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {
String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
@Override
public void onReceive(Context context, Intent intent) {
if (dStarted.equals(intent.getAction())) {
// Discovery has started.
Toast.makeText(getApplicationContext(),
"Discovery Started...", Toast.LENGTH_SHORT).show();
}
else if (dFinished.equals(intent.getAction())) {
// Discovery has completed.
Toast.makeText(getApplicationContext(),
"Discovery Completed...", Toast.LENGTH_SHORT).show();
}
}
};
registerReceiver(discoveryMonitor,
new IntentFilter(dStarted));
registerReceiver(discoveryMonitor,
new IntentFilter(dFinished));
...并在最后得到两个registerReceiver调用中的每一个。 。
令牌上的语法错误,而是期望注释名称 语法错误,插入“Type VariableDeclaratorId”以完成FormalParameterList
什么是注释名称,这里出了什么问题?
提前致谢!!
答案 0 :(得分:0)
您的问题是您将变量dStarted
和dFinished
定义为discoveryMonitor
BroadcastReceiver的本地人,
这样,你就不能在registerReceiver(...)
您必须将它们定义为全局,或使用
registerReceiver(discoveryMonitor, new
IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
代替。