- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在开发的应用程序中实现 Paho MQTT Android 服务。在测试了 Paho 提供的示例应用程序后,我发现有一些地方我想更改。
https://eclipse.org/paho/clients/android/
一旦应用程序完全关闭,应用程序服务似乎就关闭了。即使在应用程序关闭后,如果收到更多消息,我也想保持服务运行。我还在寻找一种方法,在收到新消息后将应用程序打开到特定 Activity 。
这是消息到达时调用的回调之一,我尝试实现一个简单的 startActivity 来打开特定 Activity ,但如果应用关闭/不再运行.
如果有人使用过 PAHO MQTT Android 服务,是否有特定的方法可以在应用程序关闭时防止服务停止,以及如何在消息到达时重新打开应用程序?
/**
* @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String,
* org.eclipse.paho.client.mqttv3.MqttMessage)
*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// Get connection object associated with this object
Connection c = Connections.getInstance(context).getConnection(clientHandle);
// create arguments to format message arrived notifcation string
String[] args = new String[2];
args[0] = new String(message.getPayload());
args[1] = topic + ";qos:" + message.getQos() + ";retained:" + message.isRetained();
// get the string from strings.xml and format
String messageString = context.getString(R.string.messageRecieved, (Object[]) args);
// create intent to start activity
Intent intent = new Intent();
intent.setClassName(context, "org.eclipse.paho.android.service.sample.ConnectionDetails");
intent.putExtra("handle", clientHandle);
// format string args
Object[] notifyArgs = new String[3];
notifyArgs[0] = c.getId();
notifyArgs[1] = new String(message.getPayload());
notifyArgs[2] = topic;
// notify the user
Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent,
R.string.notifyTitle);
// update client history
c.addAction(messageString);
Log.e("Message Arrived", "MESSAGE ARRIVED CALLBACK");
// used to open the application if it is currently not active
Intent i = new Intent(context, ConnectionDetails.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("handle", clientHandle);
context.startActivity(i);
}
最佳答案
虽然这似乎不是问题的完整解决方案,但我会发布我的解决方法以防它对某人有所帮助。
对我来说,当用户将应用从最近使用的应用列表中滑出时,问题就开始了。如前所述 here这样的操作不仅会终止 Activity,还会终止整个进程,包括 MqttService
。然后如线程中所述,Android 识别出您的服务应该重新启动并安排重新启动已终止的服务。但是,这并不意味着连接恢复,因为所有连接都绑定(bind)到该 Activity 。
因此,除非您找到消除服务停止问题的方法,否则当用户决定退出应用时,您肯定会失去与代理的连接。
但这并不是世界末日,因为我们可以在失去连接后轻松重新连接。问题是,这次我们没有执行所需操作的 Activity 。您必须修改 Paho Android 服务库的源代码,或者以更简单的方式创建另一个服务。
所有连接都将在这个新服务中进行,任何希望连接的 Activity 都应该与这个服务进行通信。这样做的好处是我们可以使服务具有粘性,即使用户滑动我们的应用并将其杀死,它也会立即重新启动,我们只需重新连接即可恢复。
作为这个非常简单的服务的演示:
public class MessagingService extends Service {
private static final String TAG = "MessagingService";
private MqttAndroidClient mqttClient;
String deviceId;
@Override
public void onCreate() {
}
private void setClientID() {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
deviceId = wInfo.getMacAddress();
if (deviceId == null) {
deviceId = MqttAsyncClient.generateClientId();
}
}
public class MsgBinder extends Binder {
public MsgServ getService() {
return MsgServ.this;
}
}
public void doConnect(){
// Using some of the Paho sample app classes
String server = ConfigClass.BROKER_URI;
MemoryPersistence mem = new MemoryPersistence();
mqttClient = new MqttAndroidClient(this,ConfigClass.BROKER_URI,deviceId,mem);
MqttConnectOptions conOpt = new MqttConnectOptions();
String clientHandle = server + deviceId;
Connection con = new Connection(clientHandle, deviceId, ConfigClass.BROKER_ADDRESS,
ConfigClass.BROKER_PORT, this, mqttClient, false);
conOpt.setCleanSession(false);
conOpt.setConnectionTimeout(ConfigClass.CONN_TIMEOUT);
conOpt.setKeepAliveInterval(ConfigClass.CONN_KEEPALIVE);
conOpt.setUserName("testclient");
conOpt.setPassword("password".toCharArray());
String[] actionArgs = new String[1];
actionArgs[0] = deviceId;
final ActionListener callback =
new ActionListener(this, ActionListener.Action.CONNECT, clientHandle,
actionArgs);
mqttClient.setCallback(new MqttCallbackHandler(this, clientHandle));
mqttClient.setTraceCallback(new MqttTraceCallback());
con.addConnectionOptions(conOpt);
Connections.getInstance(this).addConnection(con);
try {
mqttClient.connect(conOpt, null, callback);
Log.d("Con", "Connected");
} catch (MqttException e) {
Log.d("Con", "Connection failed");
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
doConnect();
return START_STICKY;
}
}
服务器日志:
1433455371: New client connected from 192.168.2.5 as ed:0a:2b:56:b5:45 (c0, k30, u'testclient').
1433455371: Sending CONNACK to ed:0a:2b:56:b5:45 (1, 0)
1433455375: Socket error on client ed:0a:2b:56:b5:45, disconnecting.
1433455377: New connection from 192.168.2.5 on port 1883.
1433455377: Client ed:0a:2b:56:b5:45 disconnected.
1433455377: New client connected from 192.168.2.5 as ed:0a:2b:56:b5:45 (c0, k30, u'testclient').
1433455377: Sending CONNACK to ed:0a:2b:56:b5:45 (1, 0)
如您所见,一旦我关闭应用程序并且服务被终止,它会重新启动重新连接并保持 Activity 状态,然后才找到。从这里开始,您应该可以完成剩下的工作。也许使用您新收到的消息创建一个通知,这将打开应用程序。请记住在保证保持连接的新创建服务中执行所有操作。
关于android - Paho MQTT Android 服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28543569/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!