1

我是新來的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> 

回答

0

我認爲你的代碼不會在3秒內正確地暫停。看起來好像您正在啓動另一個線程來執行延遲,這意味着您的代碼將在執行任何操作之前註冊和取消註冊接收方。

最好不要使用延遲,而是在實際的BroadcastReceiver onReceive函數中取消註冊接收器。爲此,您可能需要在擴展類Extension.context中存儲對上下文的引用(儘管您可能已經這樣做了)。

另外我建議你將BroadcastReceiver分成一個單獨的類,以保持代碼更清晰,並且應該從比FREFunction更全局的地方引用它,例如在FREContext中。我只寫一些僞代碼,在這裏給你一個想法:

擴展:

public class ANEExtension implements FREContext 
{ 
    public static FREContext context; 

    public FREContext createContext(String arg0) 
    { 
     context = new ANEContext(); 
     return context; 
    } 

    // Other functions... 
} 

語境:

public class ANEContext implements FREContext 
{ 
    public WifiReceiver receiver = null; 

    public void registerWifiReceiver() 
    { 
     if (receiver == null) 
     { 
      receiver = new WifiReceiver(); 
      IntentFilter filter = new IntentFilter(); 
      filter.addAction("com.example.Obtain_WiFi_Data"); 
      getActivity().registerReceiver(receiver, filter); 
     } 
    } 

    public void unregisterWifiReceiver() 
    { 
     if (receiver != null) 
     { 
      getActivity().unregisterReceiver(receiver); 
      receiver = null; 
     } 
    } 

    // .. Map<> getFunctions etc... 
} 

功能類:

public class WiFi_info implements FREFunction 
{ 
    @Override 
    public FREObject call(FREContext context, FREObject[] object) 
    { 
     ((ANEContext)context).registerWifiReceiver(); 

     // return stuff... 
     return NULL; 
    } 
} 

接收器類:

public class WifiReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     if(intent.getAction().equalsIgnoreCase(WiFi_Data)) 
     { 
      connectionData = intent.getStringExtra(KEY_WiFi_Info); 

      // 
      // Return Data 
      if (ANEExtension.context) 
      { 
       ANEExtension.context.dispatchStatusEventAsync( 
        "some:event:type", 
        connectionData 
       ); 
      } 
      // 
      // 


      ((ANEContext)ANEExtension.context).unregisterWifiReceiver(); 
     } 
    } 
} 

編輯:增加一個狀態事件偵聽器在ActionScript端

以下是所有AS3代碼總結從本機代碼接收狀態事件的重要組成部分。

public class WifiExtension extends EventDispatcher 
{ 
    public static const EXT_CONTEXT_ID : String = "the.extension.id"; 

    public function WifiExtension() 
    { 
     _extContext = ExtensionContext.createExtensionContext(EXT_CONTEXT_ID, null); 
     _extContext.addEventListener(StatusEvent.STATUS, extension_statusHandler, false, 0, true); 
    } 

    private function extension_statusHandler(event:StatusEvent):void 
    { 
     switch (event.code) 
     { 
      case "some:event:type": 
       trace("The data from the native code = " + event.level); 

     } 
    } 

} 
+0

我很感謝您的回覆,對不起,我沒有及時回覆。我嘗試了你的想法,但不幸的是它沒有奏效。此外,在您創建的BroadcastReceiver中,您沒有將從接收到的意圖獲得的字符串發送到WiFi_info()函數,以便它可以傳遞給用戶,這是我需要ANE執行的操作。你有什麼其他想法可以嘗試嗎? –

+0

什麼都不起作用,我在我自己的幾個ANE中使用了這種精確的技術。關於發回數據,您不能將其發送回該函數,您需要使用異步事件將其發送回Actionscript。我將編輯答案以顯示示例 – Michael

+0

我的意思是將數據發送到ActionScript端的過程不起作用。我也嘗試了你的建議,將數據作爲事件的一部分發送出去。但是,這並沒有奏效。我對ActionScript還很陌生,所以我可能沒有正確實現它。您能否提供一個如何通過事件發送數據的例子? –

相關問題