gpt4 book ai didi

android - 磨损 Activity 中的 MessageApi.MessageListener 未触发

转载 作者:行者123 更新时间:2023-11-29 01:39:01 24 4
gpt4 key购买 nike

所以我在 StackOverflow 上四处寻找,但没有找到答案,所以我想知道是否缺少某些东西。我在我的应用程序的“磨损”部分中有一个 Activity ,其包和应用程序名称与我的移动部分相同,但在发送消息时它没有被触发:

public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {

private GoogleApiClient mApiClient;
private ArrayAdapter<String> mAdapter;

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list);

mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
mListView.setAdapter( mAdapter );

initGoogleApiClient();
}

private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();

if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
mApiClient.connect();
}

@Override
protected void onResume() {
super.onResume();
if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
mApiClient.connect();
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
mAdapter.add( new String( messageEvent.getData() ) );
mAdapter.notifyDataSetChanged();

Toast.makeText( this, "Wear onMessageReceived", Toast.LENGTH_SHORT ).show();
}

@Override
public void onConnected(Bundle bundle) {
Toast.makeText( this, "Wear onConnected", Toast.LENGTH_SHORT ).show();
Wearable.MessageApi.addListener( mApiClient, this );
}

@Override
protected void onStop() {
super.onStop();
if( mApiClient != null && mApiClient.isConnected() ) {
mApiClient.disconnect();
}
}

@Override
public void onConnectionSuspended(int i) {

}

从我的移动 Activity 中,我发送了一条我希望从 Wear Activity 中获取的消息。如果我使用 Wea​​rListenerService,则 onMessageReceived 会正常触发,但我无法让它在 Activity 中触发。

public class MainActivity extends Activity {

private GoogleApiClient mApiClient;

private ArrayAdapter<String> mAdapter;

private ListView mListView;
private EditText mEditText;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();
initGoogleApiClient();
}

private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();

mApiClient.connect();
}

@Override
protected void onDestroy() {
super.onDestroy();
mApiClient.disconnect();
}

private void init() {
mListView = (ListView) findViewById( R.id.list_view );
mEditText = (EditText) findViewById( R.id.input );
mSendButton = (Button) findViewById( R.id.btn_send );

mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
mListView.setAdapter( mAdapter );

mSendButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = mEditText.getText().toString();
if( !TextUtils.isEmpty( text ) ) {
mAdapter.add( text );
mAdapter.notifyDataSetChanged();

sendMessage( text );
}
}
});
}

private void sendMessage( final String text ) {
new Thread( new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
for(Node node : nodes.getNodes()) {
Log.e("MainActivity", "node id: " + node.getId() );
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mApiClient, node.getId(), text, null).await();
}

runOnUiThread( new Runnable() {
@Override
public void run() {
mEditText.setText( "" );
}
});
}
}).start();
}

有人对我可能遗漏的内容有任何建议吗?我可以回退到使用服务,但如果可以的话,我想尝试让它与 Activity 一起使用。

谢谢!

最佳答案

所以事实证明 onMessageReceived 在它自己的线程上运行,而不是 UI 线程,所以你必须在 runOnUiThread 中包装 toast 消息和任何你自己的 UI 代码。

关于android - 磨损 Activity 中的 MessageApi.MessageListener 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25987014/

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