android 中休眠问题?

bigbat 2019-06-04 09:38:48
最近在做android mqtt客户端的程序,利用 paho.mqtt.java.example例子修改出来的,虽然能够勉强运行。但是问题太多!
1、首相这个程序要是程序一启动开始就发消息,其它客户端就可以正常接受消息。但是屏幕一变黑就偶尔可以发过去,日志多次记录有重联的情况。
2、一旦启动后是开始发信息,接收就不正常。偶尔可以接收消息。
3、如果没有在启动后发信息,接收偶尔比较正常。大部分时间都会丢消息,日志,提示没有出现重联的记录
4、只要手机出现开始休眠的症状时,就是屏幕变暗时,无论什么情况收或是发都不正常。
5、屏幕彻底变黑后,只要唤醒,日志提示重联成功,但是收或发都是偶尔正常。
综上述:只有发送消息程序偶尔正常,其它情况都不正常。
从网上反馈的资料来看是由于android的休眠机制造成的。很多资料说明android的socket程序不好保活。
大家给点建议!
程序代码

/*******************************************************************************
* Copyright (c) 1999, 2016 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*/
package paho.mqtt.java.example;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.ArrayList;

public class PahoExampleActivity extends AppCompatActivity{
private HistoryAdapter mAdapter;

MqttAndroidClient mqttAndroidClient;

final String serverUri = "tcp://192.168.1.103:1883";

String clientId = "ExampleAndroidClient";
final String subscriptionTopic = "HSLU/test";
final String publishTopic = "HSLU/subtopic/relayA";
final String publishMessage = "on";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
publishMessage();
}
});


RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.history_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new HistoryAdapter(new ArrayList<String>());
mRecyclerView.setAdapter(mAdapter);

clientId = clientId + System.currentTimeMillis();

mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId);
mqttAndroidClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {

if (reconnect) {
addToHistory("Reconnected to : " + serverURI);
// Because Clean Session is true, we need to re-subscribe
subscribeToTopic();
} else {
addToHistory("Connected to: " + serverURI);
}
}

@Override
public void connectionLost(Throwable cause) {
addToHistory("The Connection was lost.");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
addToHistory("Incoming message: " + new String(message.getPayload()));
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {

}
});

MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(false);







try {
//addToHistory("Connecting to " + serverUri);
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(100);
disconnectedBufferOptions.setPersistBuffer(false);
disconnectedBufferOptions.setDeleteOldestMessages(false);
mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic();
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
addToHistory("Failed to connect to: " + serverUri);
}
});


} catch (MqttException ex){
ex.printStackTrace();
}

}

private void addToHistory(String mainText){
System.out.println("LOG: " + mainText);
mAdapter.add(mainText);
Snackbar.make(findViewById(android.R.id.content), mainText, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement

return super.onOptionsItemSelected(item);
}

public void subscribeToTopic(){
try {
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
addToHistory("Subscribed!");
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
addToHistory("Failed to subscribe");
}
});

// THIS DOES NOT WORK!
mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// message Arrived!
System.out.println("Message: " + topic + " : " + new String(message.getPayload()));
}
});

} catch (MqttException ex){
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}

public void publishMessage(){

try {
MqttMessage message = new MqttMessage();
message.setPayload(publishMessage.getBytes());
mqttAndroidClient.publish(publishTopic, message);
addToHistory("Message Published");
if(!mqttAndroidClient.isConnected()){
addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer.");
}
} catch (MqttException e) {
System.err.println("Error Publishing: " + e.getMessage());
e.printStackTrace();
}
}

}


我没有发现可以防止休眠的代码。
...全文
135 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
乂曱酱 2019-06-05
  • 打赏
  • 举报
回复
<!-- 允许应用在手机屏幕关闭后后台进程仍然运行 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />



可以去了解下这个权限
Jimmy_buer 2019-06-05
  • 打赏
  • 举报
回复
禾火山 2019-06-04
  • 打赏
  • 举报
回复
使用 WakeLock 试试

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧