gpt4 book ai didi

java - Smack Android,创建接收消息的服务

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:02 25 4
gpt4 key购买 nike

我正在与 Android 4.1.4 的图书馆聊天。这个库使用 XMPP 协议(protocol)。要接收消息,您必须向服务器(例如 openfire)进行身份验证并使用 XMPPCONNECTION 登录。如果在应用程序启动时执行所有这些都非常简单。当您必须在应用程序关闭时接收消息时,问题就来了。我尝试使用“Android 服务”来维持客户端和服务器之间的连接。 (在这种情况下我做了)但我认为这不是最好的方法。也因为Android通过服务在手机关机再开机时服务不会自行重启,手机关机时收到的消息会丢失。我附上代码Android。你有什么建议吗?了解如何使用其他聊天应用程序(如 whatsapp、badoo、facebook、telegram 等)会很有用。

public class ServizioMessaggi extends Service {

public static final int NOTIFICATION_ID = 1;
static ChatManager chatmanager;
public static AbstractXMPPConnection connessione;
ConnettiServizio connetti;
MySQLiteHelper db;
String SharedPreferences = "Whisper";

public ServizioMessaggi() {
super();
}

@Override
public void onCreate() {

SharedPreferences sharedPref = getSharedPreferences(SharedPreferences, Context.MODE_PRIVATE);
connetti = new ConnettiServizio();
connetti.execute(sharedPref.getString("username",""),sharedPref.getString("password",""),"vps214588.ovh.net");
db = new MySQLiteHelper(this);



super.onCreate();
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}


@Override
public void onDestroy() {
super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

public class ConnettiServizio extends AsyncTask<String,String,String> {
public AbstractXMPPConnection con;
@Override
protected String doInBackground(String... strings) {
con = new XMPPTCPConnection(strings[0],strings[1],strings[2]);
try {
con.connect();
con.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

connessione = con;

con.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
System.out.println("connected");
}

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
System.out.println("autenticathed");
}

@Override
public void connectionClosed() {
System.out.println("Connection Close");
}

@Override
public void connectionClosedOnError(Exception e) {
System.out.println("Connection Close whith error");
}

@Override
public void reconnectionSuccessful() {
System.out.println("reconnection ");
}

@Override
public void reconnectingIn(int seconds) {

}

@Override
public void reconnectionFailed(Exception e) {
System.out.println("recconnection failed");
}
});

ascolta();

}
}

private void ascolta() {

chatmanager = ChatManager.getInstanceFor(connetti.con);

chatmanager.addChatListener(new ChatManagerListener() {


public void chatCreated(final Chat chat, final boolean createdLocally) {

Log.i("chat creata", "****************");

chat.addMessageListener(new ChatMessageListener() {


public void processMessage(Chat chat, Message message) {



Log.i("messaggio arrivato", "****************");

//JOptionPane.showMessageDialog(null, "Rec: For " + chat.getParticipant() + " from " + message.getFrom() + "\n" + message.getBody());

String sender = message.getFrom();


System.out.println("Received message: " + (message != null ? message.getBody() : "NULL"));

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(ServizioMessaggi.this, Chat.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(ServizioMessaggi.this, 0,
notificationIntent, 0);

// scelta suoneria per notifica
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(ServizioMessaggi.this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setColor(Color.argb(0,0,176,255))
.setTicker("Nuovo messaggio da " + message.getFrom())
.setContentTitle(sender.substring(0,sender.indexOf("@")))
.setContentText(message.getBody())
.setContentIntent(intent)
.setSound(sound);

// effettua la notifica
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());




SimpleDateFormat s = new SimpleDateFormat("hh:mm:ss");
String ora = s.format(new Date());

//aggiungo il messaggio al database
Messaggio ms = new Messaggio();
ms.setUsername(message.getFrom().substring(0, message.getFrom().indexOf("/")));
ms.setIsmy("no");
ms.setTimestamp(ora);
ms.setMessaggio(message.getBody());
db.addMessaggio(ms);


if(ChatActivity.isvisible){

((Activity)ChatActivity.c).runOnUiThread(new Runnable() {
@Override
public void run() {
ChatActivity.updateListMessaggi();
}
});
} else {

}




}

});

}

});


}


}

最佳答案

其实我还没有解决这类问题。但是,对于这种情况,我确实有办法将 BackgroundService 更改为 ForegroundService。请记住,这不会解决任何 Android 设备问题。

与 whatsapp、telegram、fb 不同。他们使用 Native C (NDK) 来强制允许应用程序在后台服务中运行的操作系统。

您可以深入了解此问题的 Telegram 源代码。

关于java - Smack Android,创建接收消息的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720023/

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