我试图触发按钮点击智能手表,然后在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>
答案 0 :(得分: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);
}
}
}
缺少某些部分(取消注册信使,解除绑定服务),但这应该是主要部分。
如果您在注册BroadcastReceiver时遇到问题,这篇文章可以提供帮助:https://stackoverflow.com/a/10851904/3047078
也许你甚至可以在没有服务的情况下通过创建一个BroadcastReceiver作为你活动的内部类来实现它。
答案 1 :(得分: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
protected void onNewIntent(Intent intent) {
String anyData = intent.getStringExtra("anyData");
}
这样它就可以与正在运行的活动进行通信,也可以创建一个新活动,如果还没有运行的话。