重要的是,我的设备是我正在测试的huawei p8 lite.
在这里:
•我有一个套接字Io库,它正在我创建的服务中初始化
•我将侦听器设置为套接字io以获取新消息
•如果应用程序未在用户的进程中被杀死,则所有内容都像char一样工作.
•服务的目的是即使应用程序被终止也要保持套接字IO连接处于活动状态,以便向用户通知新消息.
•一旦应用程序被终止,Socket IO连接就会断开连接
无论我尝试什么,我都会尝试所有可能的方法:隔离服务流程,为服务提供另一个流程,启动它粘性,启动它不会在服务破坏等时重新创建等等.
唯一有效的是startForeground()方法,但我不想使用它,因为它将遵循设计原则,而且我不想显示有关正在运行的服务的任何通知.
我的问题是下一个:任何人都可以帮助我并告诉我如何保持Socket IO连接活着,即使应用程序被杀死了?
这是服务的代码.
package com.github.nkzawa.socketio.androIDchat;import androID.app.Notification;import androID.app.notificationmanager;import androID.app.PendingIntent;import androID.app.Service;import androID.content.Context;import androID.content.Intent;import androID.os.IBinder;import androID.support.annotation.Nullable;import androID.support.v4.app.NotificationCompat;import androID.util.Log;import androID.Widget.Toast;import java.net.URISyntaxException;import io.socket.clIEnt.IO;import io.socket.clIEnt.socket;import io.socket.emitter.Emitter;public class SocketService extends Service { private Socket mSocket; public static final String TAG = SocketService.class.getSimplename(); @OverrIDe public IBinder onBind(Intent intent) { // Todo: Return the communication channel to the service. throw null; } @OverrIDe public voID onCreate() { super.onCreate(); Toast.makeText(this,"on created",Toast.LENGTH_SHORT).show(); } @OverrIDe public int onStartCommand(Intent intent,int flags,int startID) { Toast.makeText(this,"start command",Toast.LENGTH_SHORT).show(); try { mSocket = IO.socket(Constants.CHAT_SERVER_URL); } catch (URISyntaxException e) { throw new RuntimeException(e); } mSocket.on("newMessageReceived",onNewMessage); mSocket.connect(); return START_STICKY; } private Emitter.Listener onNewMessage = new Emitter.Listener() { @OverrIDe public voID call(Object... args) { String message = args[0].toString(); Log.d(TAG,"call: new message "); sendGeneralNotification(getApplicationContext(),"1","new message",message,null); } }; private voID sendGeneralNotification(Context context,String uniqueID,String Title,String contentText,@Nullable Class<?> resultClass) { notificationmanager notificationmanagerCompat = (notificationmanager) context.getSystemService(NOTIFICATION_SERVICE); androID.support.v7.app.NotificationCompat.Builder builder = new androID.support.v7.app.NotificationCompat.Builder(context); builder.setautoCancel(true); builder.setContentTitle(Title); builder.setContentText(contentText); builder.setGroup("faskfjasfa"); builder.setDefaults(androID.app.Notification.DEFAulT_ALL); builder.setStyle(new NotificationCompat.BigTextStyle() .setSummaryText(Title) .setBigContentTitle(Title) .bigText(contentText) ); Intent requestsVIEwIntent = new Intent(context,MainActivity.class); requestsVIEwIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top | Intent.FLAG_ACTIVITY_SINGLE_top | Intent.FLAG_ACTIVITY_NEW_TASK); requestsVIEwIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top | Intent.FLAG_ACTIVITY_SINGLE_top | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent requestsVIEwPending = PendingIntent.getActivity(context,Integer.valueOf(uniqueID),requestsVIEwIntent,0); builder.setContentIntent(requestsVIEwPending); builder.setSmallicon(R.drawable.ic_launcher); builder.setShowWhen(true); androID.app.Notification notification = builder.build(); notificationmanagerCompat.notify(Integer.valueOf(uniqueID),notification); } private Notification getNotification() { Notification notification; NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setcolor(getResources() .getcolor(R.color.material_deep_teal_500)) .setautoCancel(true); notification = builder.build(); notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_auto_CANCEL; return notification; }}
不,我不想既不使用firebase也不想使用任何第三方制造的,因为我现在正在使用一个,而且我受到延迟,未收到的通知等等,我将使我自己的小方法更好.感谢大家的时间和耐心.
解决方法 很抱歉,没有人快速回复您的问题.我希望情人节2018不会花在别人的痛苦上.在后台实现Socket.io并不是最好的想法. Socket.io对您的服务器有一个活动的Ping.在短时间内使用它是可以的,但在后台它是NO.
解决问题的最佳方法是结合Socket.io和FCM / GCM主题发出广播.在用户设备上,检测是否存在活动的Socket.io连接,如果退出则忽略FCM,否则执行FCM.
总结以上是内存溢出为你收集整理的使用Android服务的套接字Io全部内容,希望文章能够帮你解决使用Android服务的套接字Io所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)