2017-07-18 3 views
3

Codename Oneでテストアプリケーションを作成しようとしていますが、ダーウィン通知によるdisplayStatusの変更を待ち受け、イベントをコールバック経由でJava側に送信します。私はCの知識がほとんどなく、客観的なCの知識もほとんどないので、その部分のコードのほとんどがウェブのいくつかの場所から持ち上げられ、参加しました。私は開発者ガイドに従っていますが、ビルドはクラウド上で失敗します。これまでのところ、私は次のことを行っている:ダーウィン通知に登録し、コールバック経由でイベントの詳細を送信する - コードネームワン

私のstartメソッドでは私はこれを持っている:

public void start() { 
    if(current != null){ 
     current.show(); 
     return; 
    } 
    Form hi = new Form("Xerveur", new LayeredLayout()); 

    hi.add(buildRootContainer()).add(buildRootChildContainer()); 

    Display.getInstance().callSerially(hi::show); 
    registerForNativeCallback(); 
} 

これは、ネイティブインタフェースのための登録:

private void registerForNativeCallback(){ 
    NativeListener listener = NativeLookup.create(NativeListener.class); 
    if(listener != null && listener.isSupported()){ 
     Log.p("Setup Event Listener returned: " + listener.setupEventListener()); 
    } 
} 

上記のコードスニペットは、メインクラスでありますファイル。 NativeListenerインターフェースはシンプルです:

public interface NativeListener extends NativeInterface { 
    public boolean setupEventListener(); 
} 

は、今私は、ネイティブ側から必要な情報を持つ、文字列を受け取ることになっているシンプルなコールバッククラスを持っている:

public class NativeCallback { 
    public static void receive(String payload){ 
     Log.p(payload); 
    } 
} 

これらは内容です生成された「.M」ファイル(「.H」ファイルが自動生成されたものとは変更されていない)と私が編集:

#import "ca_ratelsoft_testing_testapp2_NativeListenerImpl.h" 
#include "ca_ratelsoft_testing_testapp2_NativeCallback.h" 
#include "CodenameOne_GLViewController.h" 
#include <unistd.h>  // good idea in general 
#include <stdlib.h>  // good idea in general 

#include <CoreFoundation/CoreFoundation.h> 
#include <notify.h>  // for all notifications 


@implementation ca_ratelsoft_testing_testapp2_NativeListenerImpl 

-(BOOL)setupEventListener{ 
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center 
             NULL, // observer 
             displayStatusChanged, // callback 
             CFSTR("com.apple.springboard.displayStatus"), // event name 
             NULL, // object 
             CFNotificationSuspensionBehaviorDeliverImmediately); 

    return YES; 
} 

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { 
    NSLog(@"event received!"); 
    // you might try inspecting the `userInfo` dictionary, to see 
    // if it contains any useful info 
    if (userInfo != nil) { 
     const void * keys; 
     const void * values; 
     NSString *payload = @"displayStatus$$$"; //delimeter: $$$ 
     CFDictionaryGetKeysAndValues(userInfo, &keys, &values); 

     //key1=value1;key2=value2; 

     for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) { 
      const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding()); 
      const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding()); 
      if(i > 0) 
       payload = [payload stringByAppendingString:@";"]; 
      payload = [payload stringByAppendingString:@(keyStr)]; 
      payload = [payload stringByAppendingString:@"="]; 
      payload = [payload stringByAppendingString:@(valStr)]; 
     } 

     ca_ratelsoft_testing_testapp2_NativeCallback_receive___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG payload)); 
    } 
} 

-(BOOL)isSupported{ 
    return YES; 
} 

@end 

をデバッグiOS版を構築するとき、私は次のエラーを取得しますアプリ:https://www.dropbox.com/s/sq8f00nzf445gp0/f9e35511-c43f-4bb6-854a-f513ec8e3820-1500397464685-error.txt?dl=0

答えて

1

最初にエラーで開始します。下にスクロールするとNativeListenerImpl.oが表示されます。あなたがファイルにNativeListenerImplを検索する場合、あなたは右のそれを下回ることと実際のエラーのためにコンパイルコードが表示されます。

src/ca_ratelsoft_testing_testapp2_NativeListenerImpl.m:2:10: fatal error: 'ca_ratelsoft_testing_testapp2_NativeCallback.h' file not found 
#include "ca_ratelsoft_testing_testapp2_NativeCallback.h" 
     ^
1 error generated. 

たちのオプティマイザが終わっ未使用のコードを削除することを熱望していると使用方法を見つけることができないので、これが起こりますコールバックのこれを解決するには、メインクラスに次のコードを追加します。

boolean fakeVariable; 

public void init(Object o) { 
    // ... rest of code 

    if(fakeVariable) { 
     NativeCallback.receive(null); 
    } 
} 

これは重要です。変数をプライベートにしないでください!!!

変数はパッケージで保護されており、コードは決して発生しないように常にfalseになります。理論的には、一部のコードではそのフラグを変更できるため、オプティマイザはそれを検出できず、そのコードをそのまま残すことになります。

+0

ありがとうございます。それはそのエラーを過ぎて働いた。 – Richboy

関連する問題