Android 3、registerReceiver和getApplicationContext函数的处理不存在
我正忙着拼凑一个应用程序,在Android设备和Arduino之间传输一些数据 原始代码取自教程,是为处理API 10开发的2.0b而编写的 我正在使用Processing 3,为API 15开发 代码如下:Android 3、registerReceiver和getApplicationContext函数的处理不存在,android,function,compiler-errors,processing,Android,Function,Compiler Errors,Processing,我正忙着拼凑一个应用程序,在Android设备和Arduino之间传输一些数据 原始代码取自教程,是为处理API 10开发的2.0b而编写的 我正在使用Processing 3,为API 15开发 代码如下: /* DiscoverBluetooth: Written by ScottC on 18 March 2013 using Processing version 2.0b8 Tested on a Samsung Galaxy SII, with Android version 2
/* DiscoverBluetooth: Written by ScottC on 18 March 2013 using
Processing version 2.0b8
Tested on a Samsung Galaxy SII, with Android version 2.3.4
Android ADK - API 10 SDK platform */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import android.view.Gravity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
boolean foundDevice=false; //When this is true, the screen turns green.
//Get the default Bluetooth adapter
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
/*The startActivityForResult() within setup() launches an
Activity which is used to request the user to turn Bluetooth on.
The following onActivityResult() method is called when this
Activity exits. */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==0){
if(resultCode == -1){
ToastMaster("Bluetooth has been switched ON");
} else {
ToastMaster("You need to turn Bluetooth ON !!!");
}
}
}
/* Create a Broadcast Receiver that will later be used to
receive the names of Bluetooth devices in range. */
BroadcastReceiver myDiscoverer = new myOwnBroadcastReceiver();
void setup(){
orientation(LANDSCAPE);
/*IF Bluetooth is NOT enabled, then ask user permission to enable it */
if (!bluetooth.isEnabled()) {
Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(requestBluetooth, 0);
}
/*If Bluetooth is now enabled, then register a broadcastReceiver to report any
discovered Bluetooth devices, and then start discovering */
if (bluetooth.isEnabled()) {
registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
//Start bluetooth discovery if it is not doing so already
if (!bluetooth.isDiscovering()){
bluetooth.startDiscovery();
}
}
}
void draw(){
//Display a green screen if a device has been found
if(foundDevice){
background(10,255,10);
}
}
/* This BroadcastReceiver will display discovered Bluetooth devices */
public class myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
//Display the name of the discovered device
ToastMaster("Discovered: " + discoveredDeviceName);
//Change foundDevice to true which will make the screen turn green
foundDevice=true;
}
}
/* My ToastMaster function to display a messageBox on the screen */
void ToastMaster(String textToDisplay){
Toast myMessage = Toast.makeText(getApplicationContext(),
textToDisplay,
Toast.LENGTH_LONG);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}
我在第57行和第93行收到编译错误,说明:
The function "registerRevceiver(BroadcastReceiver, IntentFilter)" does not exist
The function "getApplicationContext()" does not exist
对于如何让这个代码示例工作,有人有什么想法吗?我怀疑这是一个与API级别和某些类型的库不匹配有关的问题
任何帮助都将不胜感激
Jono而不是
getApplicationContext()
使用ActivityName。感谢您的回复。我尝试了一下,但我得到了另一个编译器错误:“类“ActivityName”不存在”。有什么我遗漏的吗?ThanksI的意思是把你活动的班级名称放在那里。主要活动或是其他什么,我不知道它叫什么。你能看一下密码并告诉我在哪里可以找到它吗?那会有帮助的。还有,为什么我不能使用getApplicationContext()?我现在才意识到这不是Android应用,对不起。尽管如此,试着只使用这个。不用担心。我试过了,然后给出了错误,函数“makeText()”需要的参数是:“makeText(Context,CharSequence,int)”
。无论如何谢谢你的帮助。