1

나는 smartwatch에 단추를 클릭하고 그 후에 안드로이드 전화에있는 호스트 신청에있는 원본을 바꾸기 위하여 방아쇠를 당기는 것을 시도하고있다. 브로드 캐스트 인 텐트를 브로드 캐스트 리시버에 보내고 호스트 응용 프로그램의 텍스트를 변경하는 메서드가 포함 된 서비스를 시작하려고했습니다. 그러나 changeText() 메서드가 작동하지 않는 것 같습니다. 서비스를 시작할 수는 있지만 텍스트를 변경할 수는 없습니다. 제 코드에서 무엇이 잘못되었는지보십시오. 모범 사례로 smartwatch에서 호스트 응용 프로그램으로 브로드 캐스트 인 텐트를 보내는 방법에 대한 간단한 예제를 제공 할 수 있다면 좋을 것입니다.sony smartwatch 2에서 호스트 응용 프로그램의 텍스트를 변경하는 방법은 무엇입니까?

내 제어 확장 클래스

class SampleControlSmartWatch2 extends ControlExtension { 
    // Other code 

    public void onObjectClick(ControlObjectClickEvent event) { 
     Intent intent = new Intent(SampleExtensionService.INTENT_ACTION_BROADCAST); 
     intent.putExtra("Name", "something"); 
     mContext.sendBroadcast(intent); 
    }  
} 

내 방송 수신기

public class ExtensionReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     intent.setClass(context, HostService.class); 
     context.startService(intent);  
    } 
} 

내 호스트 응용 프로그램 서비스

public class HostService extends Service { 
    // Other code 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     changeText();   
    } 

    public void changeText() { 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layoutHost = inflater.inflate(R.layout.activity_main, null);  
     TextView textView = (TextView) layoutHost.findViewById(R.id.textToBeChanged); 
     Log.i(TAG, textView.getText().toString());  // It shows "Original" 
     textView.setText("Changed Text"); 
     Log.i(TAG, textView.getText().toString());  // It shows "Changed Text" 
    } 
} 

내 AndroidManifest.xml을

<application 
    <!-- Other attribute --> 

    <service android:name="com.sony.samplecontrolnotification.SampleExtensionService" /> 
    <service android:name="com.sony.samplecontrolnotification.HostService" /> 

    <receiver android:name="com.sony.samplecontrolnotification.ExtensionReceiver" > 
     <intent-filter> 
      <action android:name="com.sony.samplecontrolnotification.BROADCAST" />  
      <!-- Other action -->     
     </intent-filter> 
    </receiver> 
</application> 
내 활동에

: 그것이 가장 좋은 방법은 있지만 내가 그것을 해결하는 방법 경우 6,
+0

로그 출력을 추가하여 의도를 전혀 수신하지 못 했습니까? –

+0

나는 시도하고 의도는 예상대로받습니다. HostService onCreate() 메서드도 호출됩니다. 또한, Log.i (TAG, textView.getText(). toString())에 의해 textView의 텍스트를 로깅하려고 시도했지만 올바른 것입니다. –

+1

저는 레이아웃 인플레이터가 새로운 레이아웃을 생성하지만 이것이 화면에 표시된 레이아웃이 아니라는 것이 문제라고 생각합니다. 나는 메신저 인스턴스를 활동에서 서비스로 전달함으로써 그것을 해결했다. 따라서 서비스는 활동에 메시지를 보내고 활동은보기를 변경할 수 있습니다. –

답변

1

확실하지 내 서비스에서

private MyService mService; 
final Messenger mMessenger = new Messenger(new IncomingHandler(this)); 

@Override 
protected void onStart() { 
    super.onStart(); 
    Intent intent = new Intent(this, MyService.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
} 

private ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     LocalBinder binder = (LocalBinder) service; 
     mService = binder.getService(); 
     mService.RegisterMessenger(mMessenger); 
     mBound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     mBound = false; 
    } 
}; 

public static class IncomingHandler extends Handler { 
    private final WeakReference<MyActivity> activity; 

    IncomingHandler(MyActivity activity) { 
     this.activity = new WeakReference<MyActivity>(activity); 
    } 

    @Override 
    public void handleMessage(Message msg) { 
     MyActivity dialog = activity.get(); 
     Bundle data = msg.getData(); 

     //TODO: update view here 
    } 
}; 


:

private ArrayList<Messenger> messengers = new ArrayList<Messenger>(); 

public class LocalBinder extends Binder { 
    public MyService getService() { 
     return MyService.this; 
    } 
} 

public void RegisterMessenger(Messenger messenger) 
{  
    messengers.add(messenger); 
} 

public class MyReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     for (Messenger messenger : messengers) 
     {   
      Message m = new Message(); 
      Bundle data = new Bundle(); 
      //TODO: add data to the message here 

      messenger.send(m); 
     } 
    } 
} 

이 몇 가지 누락 된 부분 (메신저 등록 취소, 서비스 해제)하지만 이것은 주요 부분이어야합니다.

당신이 브로드 캐스트 리시버를 등록에 문제가있는 경우,이 게시물이 도움이 될 수 있습니다 https://stackoverflow.com/a/10851904/3047078

어쩌면 당신도 당신의 활동의 내부 클래스로 브로드 캐스트 리시버를 생성하여 서비스없이 그것을 할 수 있습니다.

+0

mService는 어디에 선언 되었습니까? –

+0

그냥 활동 (편집 된 답변) –

+0

서비스에 선언 된 (내 편집 참조) –

1

쉬운 방법은 응용 프로그램 활동과 이벤트를 보내는 방법을 Intent을 사용하는 것입니다 : 다음

private void sendEventToActivity(String anyData) { 
    Intent intent = new Intent(mContext, YourActivity.class); 

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    intent.putExtra("anyData", anyData); 

    mContext.startActivity(intent); 
} 

그리고 당신의 활동에 대한 onNewIntent를 오버라이드 (override) :

@Override 
protected void onNewIntent(Intent intent) { 
    String anyData = intent.getStringExtra("anyData"); 
} 

가 실행중인 활동이나와 통신이 방법을 아직 실행되지 않는 경우 새 것을 작성하십시오.

+0

좋고 깨끗한 솔루션! –

관련 문제