首先,我是 Android 编码的新手,逻辑真的让我很困惑。最初我是 Flash 开发人员,我熟悉那里的概念,而 Android 是一套完整的新概念。例如(如果我错了,请纠正我) Intent 就像一个 Event 而 BroadcastReceiver 是一个 EventListener ?
好吧,我被卡住了,如果是这样,Intent 是 Event 而 broadcastReceiver 是 eventListener 那么我的问题是我如何分配一个变量,我在 onReceive 方法中处理的数据?
我已经搜索了很长时间,并且因为不理解逻辑而对自己感到生气。我试图将事物与 ActionScript3 和 Javascript 进行比较和关联(JS 中的一些东西非常接近 AS3)。
现在到我试图编写的代码和我遇到的问题。
我试图让自己为 Adobe AIR 编写一个 Android 原生扩展......所以,至少在某些方面非常好 :)
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.as3breeze.air"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BluetoothExtension"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如我所见,主要活动是我的 BluetoothExtension.java 如下:注意它实现了 FREExtension(由 Adobe 为 Native Extensions 创建)
package com.as3breeze.air;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;
import com.as3breeze.air.BluetoothExtensionContext;
public class BluetoothExtension implements FREExtension {
protected BluetoothExtensionContext BEC;
/** Called when the activity is first created. */
@Override
public FREContext createContext(String arg0) {
BEC = new BluetoothExtensionContext();
return BEC;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
BEC.dispose();
BEC = null;
}
@Override
public void initialize() {}
}
那是活动,对吧?
它创建了以下上下文(我省略了@imports):
public class BluetoothExtensionContext extends FREContext {
public BluetoothAdapter bluetooth;
public Activity extensionActivity;
public FREArray nonBoundedDevices;
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Map<String, FREFunction> getFunctions() {
Map<String, FREFunction> functionMap=new HashMap<String, FREFunction>();
functionMap.put("initialize", new Initialize());
// Leaving out some stuff here and listing only the important things...
functionMap.put("listDevices", new ListAvailableDevices());
return functionMap;
}
}
现在,正如您在上面看到的,我有一些公共变量以便于访问,它们是在 new Initialize() 内部启动的,如下所示:
package com.as3breeze.air;
import android.bluetooth.BluetoothAdapter;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import com.adobe.fre.FREWrongThreadException;
import com.as3breeze.air.BluetoothExtensionContext;
public class Initialize implements FREFunction {
@Override
public FREObject call(FREContext context, FREObject[] args) {
BluetoothExtensionContext bluetoothContext = (BluetoothExtensionContext) context;
bluetoothContext.bluetooth = BluetoothAdapter.getDefaultAdapter();
bluetoothContext.extensionActivity = bluetoothContext.getActivity();
FREObject returnData = null;
if( bluetoothContext.bluetooth == null )
{
try {
returnData = FREObject.newObject("notSupported");
} catch (FREWrongThreadException e) {}
}
return returnData;
}
}
启动工作正常,我还在地图中列出了其他方法,例如启用/禁用蓝牙、可发现性等等,那里的一切都运行良好。
但问题出在这个:functionMap.put("listDevices", new ListAvailableDevices());
类被创建并运行并返回,它看起来像这样:
package com.as3breeze.air;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import com.adobe.fre.FREASErrorException;
import com.adobe.fre.FREArray;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREInvalidObjectException;
import com.adobe.fre.FREObject;
import com.adobe.fre.FRETypeMismatchException;
import com.adobe.fre.FREWrongThreadException;
public class ListAvailableDevices implements FREFunction {
static FREArray returnDevicesArr = null;
@Override
public FREObject call(FREContext context, FREObject[] args) {
BluetoothExtensionContext bluetoothContext = (BluetoothExtensionContext) context;
returnDevicesArr = bluetoothContext.nonBoundedDevices;
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int index = 0;
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice bt = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText( context, "Searching devices...", Toast.LENGTH_SHORT).show();
FREArray deviceName;
try {
deviceName = FREArray.newArray(1);
deviceName.setObjectAt(0, FREObject.newObject(bt.getName()));
deviceName.setObjectAt(1, FREObject.newObject(bt.getAddress()));
returnDevicesArr.setObjectAt(index, deviceName);
index++;
} catch (FREASErrorException e) {
e.printStackTrace();
} catch (FREWrongThreadException e) {
e.printStackTrace();
} catch (FREInvalidObjectException e) {
e.printStackTrace();
} catch (FRETypeMismatchException e) {
e.printStackTrace();
}
}
}
};
bluetoothContext.extensionActivity.registerReceiver(mReceiver, filter);
bluetoothContext.bluetooth.startDiscovery();
return null; // Or to use return returnDeviceArr;
}
}
如您所见,我试图将所有找到的设备存储在 returnDeviceArr 中,以便从 call() 或 BluetoothExtensionContext.java 中定义的某个“全局”变量中返回,走哪条路都没有关系,我只需要掌握它数据。
我无法从 onReceive 方法访问 returnDeviceArr 变量。我还测试了在 onReceive 内部创建一个新的 FREArray 并将设备数据存储在那里,因此可以返回但返回 null;在 call(){ ... } 的底部被触发并最终给我 null 值。
那么,我怎样才能用 return returnDeviceArr 替换 return null; 并获得可用设备的数组?
我希望有代码示例和解释,这样我就可以在不使用可视组件的情况下开始理解 Android 编码。