首先是Android編碼的完整新手,邏輯真的讓我困惑。最初,我是Flash開發人員,我熟悉那裏的概念,而Android則是一套完整的新概念。例如(糾正我,如果我錯了)一個意圖就像一個事件和BroadcastReceiver是一個EventListener?試圖瞭解Android編碼中的BroadcastReceiver
那麼它在這裏我堅持,如果是這樣,意圖是事件和broadcastReceiver是eventListener那麼我的問題是我如何分配一個變量,數據,我在onReceive方法handeled?
我一直在尋找很長一段時間,真的因爲不理解邏輯而對自己生氣。我試圖比較和關聯的東西到ActionScript3和JavaScript(JS中的一些東西與AS3非常接近)。
現在到代碼即時嘗試寫和我得到的問題。
我試着對自己進入編寫Android原生擴展Adobe AIR的... 所以,到目前爲止,至少以某種方式:)
清單文件好:
<?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的原生擴展創建)
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;
}
}
現在,你看上面我得到了更容易獲得一些公共瓦爾,這些都是內新初始化啓動()它看起來像這樣:
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找到的所有設備無論是從調用返回(),或在一些「全球「在BluetoothExtensionContext.java中定義的變量,走哪條路並不重要,我只需要掌握這些數據。
Im無法從onReceive方法到達returnDeviceArr變量。我也測試過在onReceive中創建一個新的FREArray,並在那裏存儲設備數據,所以它可以返回,但返回null;在call(){...}的底部被觸發並最終給我null值。
那麼,我怎樣才能使它有可能取代返回null返回returnDeviceArr;並獲得可用設備的數組?
林希望代碼示例和解釋,所以我可以開始理解Android編碼,而無需使用可視化組件。
「必須從活動類擴展」,這是否表示它不會在我的情況下工作,否則作爲AIR的本機擴展?另外,你能否給我一個代碼示例,相信我通過了文檔,但是由於有關於哪些類(哪些類)方法應該被使用的解釋,所以我覺得有點失落:/如果我要使用Intent和startactivityforresult,在哪一類我應該添加它,BluetoothExtension在那種情況下應該擴大活動?感謝您的迴應,真正期待得到Android編碼的控制權:D – Deko
ahh我還看到一件事讓我感到困惑:Intent intent = new Intent(this,target.class);什麼是target.class,它是一些其他類,還是專指「this」? – Deko
使類BluetoothExtension擴展類像這樣的公共類BluetoothExtension擴展Activity實現FREExtension { – Shiva