gpt4 book ai didi

android - 如何从 sony smartwatch 2 更改主机应用程序中的文本?

转载 作者:行者123 更新时间:2023-11-29 21:10:08 25 4
gpt4 key购买 nike

我正在尝试触发智能 watch 上的按钮点击,然后更改 Android 手机主机应用程序中的文本。我试图向广播接收器发送广播 Intent 并启动一项服务,其中包括一种更改主机应用程序中的文本的方法。但是,changeText() 方法似乎不起作用。我可以启动服务但无法更改文本。请查看我的代码中有什么问题。如果您能给出一个简短示例,说明如何在最佳实践中将广播 Intent 从智能 watch 发送到主机应用程序,那就太好了。

我的控件扩展类

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>

最佳答案

不确定这是否是最好的方法,但这是我解决它的方法:

在我的 Activity 中:

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

缺少一些部分(取消注册信使,取消绑定(bind)服务)但这应该是主要部分。

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

也许你甚至可以在没有服务的情况下通过创建一个 BroadcastReceiver 作为你的 Activity 的内部类来做到这一点。

关于android - 如何从 sony smartwatch 2 更改主机应用程序中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23198210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com