我试图触发在smartwatch上的按钮单击,然后在android手机的主机应用程序中更改文本。
我试图将广播意图发送到广播接收器并启动一项服务,该服务包括一种更改主机应用程序中文本的方法。但是,changeText()方法似乎不起作用。我能够启动服务,但无法更改文本。请查看我的代码有什么问题。如果您能举一个简短的示例,以最佳实践的方式将如何从智能手表向主机应用程序发送广播意图,将是很好的。

我的控制扩展程序类

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>

最佳答案

不知道这是否是最好的方法,但这是我解决的方法:

在我的活动中:

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);
        }
    }
}


缺少某些部分(注销Messenger,取消绑定服务),但这应该是主要部分。

如果您在注册BroadcastReceiver时遇到问题,此帖子可以提供帮助:https://stackoverflow.com/a/10851904/3047078

也许您甚至可以不创建服务而只通过创建BroadcastReceiver作为活动的内部类来执行此操作。

10-07 16:22
查看更多