首先,我是Android编码的完全新手,逻辑真的让我很困惑。最初是我的Flash开发人员和我熟悉的概念,而Android是完整的新概念集。例如(纠正我,如果我错了)一个Intent就像一个Event而BroadcastReceiver是一个EventListener?
嗯,它在这里被卡住,如果是这样,Intent是Event而broadcastReceiver是eventListener那么我的问题是我如何分配一个变量,我已经在onReceive方法中处理过的数据?
我一直在寻找很长一段时间,并且因为不理解逻辑而对自己生气。我试图比较和关联ActionScript3和Javascript(JS中的一些东西非常接近AS3)。
现在我要编写的代码和我得到的问题。
我试图让自己为Adobe AIR编写Android Native Extension ... 所以,至少在某种程度上这么好:)
清单文件:
<?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;
}
}
现在,如上所示,我有一些公共变量可以更方便地访问,这些是在新的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(){...}底部被触发并最终给我空值。
那么,我怎样才能用返回returnDeviceArr替换返回null;并获得可用设备的数组?
我希望有代码示例和解释,这样我就可以开始理解Android编码而不使用可视化组件。
答案 0 :(得分:0)
BlueToothExtension类必须扩展活动类。单单宣布清单是不够的。你应该注意你的广播接收器。查看android开发指南以获取更多详细信息。
对于传递变量的Intents,你应该把它们放在像
这样的额外内容中`Intent intent = new Intent(this,target.class);
intent.putExtra(“variable”,value);
startActivityForResult(intent,request_Code);`
在目标类中,您可以使用intent.getStringExtra(“variable”)获取变量;