我是新來的Flex移動應用程序如何工作,所以請在你的回覆中描述。如何在Flex移動應用程序中使用BroadcastReceivers?
我想爲Android創建一個Flex移動應用程序,該應用程序能夠從我創建的服務應用程序接收意圖。意圖有一個附加的字符串,這是非常重要的,我的應用程序獲取該信息。但是,我似乎無法弄清楚如何讓我的Flex應用程序接收服務應用程序發送的意圖。
到目前爲止,我已經嘗試讓應用程序在原生Android端暫停3秒,希望能夠有足夠的時間調用onReceive()。這沒有奏效。
以下是我的代碼。我感謝您的幫助。
WiFi功能向服務發送意圖以激活Android上的WiFi。
WiFi_info函數應該返回附加到該應用程序將接收的意圖的數據。
ANESimpleApp.as - ActionScript庫
package com.example.anesimpleapp
{
import flash.external.ExtensionContext;
public class ANESimpleApp
{
// This will hold the context for the Native Android side of the plugin
private var context:ExtensionContext;
// Get a context of the ANE
public function ANESimpleApp()
{
// Checks if the context was already setup
if(!context)
{
context = ExtensionContext.createExtensionContext("com.example.anesimpleapp", null);
}
}
// Turn On/Off the Wifi
public function WiFi(): void
{
context.call("WiFi", null);
}
// Get WiFi information
public function WiFi_Info(): String
{
return String (context.call("WiFi_info", null));
}
}
}
WiFi_info.java - 原生Android代碼
public class WiFi_info implements FREFunction {
// This will retrieve information about the WiFi network the phone is currently connected to
public String connectionData = "EMPTY";
// This is used to reference the string value describing the WiFi network the phone is currently connected to.
public final String KEY_WiFi_Info = "Update_WiFi_Info";
// The expected intent
public final String WiFi_Data = "com.example.Obtain_WiFi_Data";
// Created a Runnable object
Runnable myRun;
@Override
public FREObject call(FREContext context, FREObject[] object) {
// The runnable object will be used to allow enough time for the BroadcastReceiver to set itself up
myRun = new Runnable() {
@Override
public void run()
{
// Causes this activity to wait 3 second.
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// Sets up intent filter and BroadcastReceiver
// This intent filter will allow the application to receive certain intents
IntentFilter filter = new IntentFilter();
// This allows the application to receive data about the WiFi
filter.addAction(WiFi_Data);
// Registers the BroadcastReceiver onReceive() in this app
context.getActivity().registerReceiver(mybroadcast, filter);
// Generates a 3 second pause
Thread myThread = new Thread(myRun);
myThread.start();
// Returns the information obtained from onReceive()
// This will be used to hold the value returned from the function
FREObject returnValue = null;
try {
// Obtains a string containing information about the WiFi network the phone is
// currently connected to.
returnValue = FREObject.newObject(connectionData);
} catch (FREWrongThreadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
// Unregisters the BroadcastReceiver (Don't want any leaked receivers)
context.getActivity().unregisterReceiver(mybroadcast);
// Returns value
return returnValue;
}
// Receives the intent and places extra in class variable
public BroadcastReceiver mybroadcast = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equalsIgnoreCase(WiFi_Data))
{
connectionData = intent.getStringExtra(KEY_WiFi_Info);
}
}
};
}
ANESimpleAppTestHomeView.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.example.anesimpleapp.ANESimpleApp;
[Bindable(event="UpdateTime")]
private function WifiUpdate(): String
{
var ane:ANESimpleApp = new ANESimpleApp();
return ane.WiFi_info();
}
public function button1_WiFiActivation(event:MouseEvent):void
{
var ane:ANESimpleApp = new ANESimpleApp();
ane.WiFi();
}
public function button2_WiFiUpdater(event:MouseEvent):void
{
dispatchEvent(new Event("UpdateTime"));
}
]]>
</fx:Script>
<s:VGroup>
<s:HGroup>
<s:Button id="button1"
x="25"
y="27"
label="WiFi"
click="button1_WiFiActivation(event)"/>
<s:Button id="button2"
x="25"
y="27"
label="WiFi Info"
click="button2_WiFiUpdater(event)"/>
</s:HGroup>
<s:TextArea id="WiFiInfo"
width="65%"
editable="false"
borderVisible="false"
contentBackgroundColor="0xFFFFFF"
contentBackgroundAlpha="0"
height="400"
text="{WifiUpdate()}"/>
</s:VGroup>
</s:View>
我很感謝您的回覆,對不起,我沒有及時回覆。我嘗試了你的想法,但不幸的是它沒有奏效。此外,在您創建的BroadcastReceiver中,您沒有將從接收到的意圖獲得的字符串發送到WiFi_info()函數,以便它可以傳遞給用戶,這是我需要ANE執行的操作。你有什麼其他想法可以嘗試嗎? –
什麼都不起作用,我在我自己的幾個ANE中使用了這種精確的技術。關於發回數據,您不能將其發送回該函數,您需要使用異步事件將其發送回Actionscript。我將編輯答案以顯示示例 – Michael
我的意思是將數據發送到ActionScript端的過程不起作用。我也嘗試了你的建議,將數據作爲事件的一部分發送出去。但是,這並沒有奏效。我對ActionScript還很陌生,所以我可能沒有正確實現它。您能否提供一個如何通過事件發送數據的例子? –