gpt4 book ai didi

android - aSmack 即服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:47 26 4
gpt4 key购买 nike

基本上我有一个运行整个项目的主类。该代码运行良好,但一旦应用程序失去焦点,它就会变得不活动。我想知道我将如何让它成为一项服务。一个会在启动时启动的。

该应用程序将是一种用于通知的单向消息系统。即

Desktop Client -> Openfire Server -> Android XMPP Service -> Storage (DB) -> Android GUI for display

正如我所说,该代码正在运行(连接、登录、接收)但不是服务。

我可以使用 BEEM 源,但它的特色和交错太多。我正在寻找轻量级服务。

代码:

public class MainActivity extends Activity {

public static final String HOST = "fire.example.com";
public static final int PORT = 5222;
public static final String SERVICE = "example.com";
public static final String USERNAME = "metest@fire.example.com";
public static final String PASSWORD = "mepass";

private XMPPConnection connection;
private ArrayList<String> messages = new ArrayList<String>();
private Handler mHandler = new Handler();
private ListView listview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listview = (ListView) this.findViewById(R.id.listMessages);
setListAdapter();

connect();
}

/**
* Called by Settings dialog when a connection is establised with
* the XMPP server
*/
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPChatActivity ", " Text Recieved " + message.getBody() + " from " + fromName);

messages.add(message.getBody());


mHandler.post(new Runnable() {
public void run() {
setListAdapter();
}
});
}
}


}, filter);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")


private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, messages);
listview.setAdapter(adapter);
}

@Override
protected void onDestroy() {
super.onDestroy();
try {
connection.disconnect();
} catch (Exception e) {

}
}

public void connect() {

final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
Log.i("XMPPChatActivity", "[SettingsDialog] Connected to "+connection.getHost());
} catch (XMPPException ex) {
Log.e("XMPPChatActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.e("XMPPChatActivity", ex.toString());
setConnection(null);
}
try {
connection.login(USERNAME, PASSWORD);
Log.i("XMPPChatActivity", "Logged in as" + connection.getUser());

// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);

Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {

Log.d("XMPPChatActivity", "--------------------------------------");
Log.d("XMPPChatActivity", "RosterEntry " + entry);
Log.d("XMPPChatActivity", "User: " + entry.getUser());
Log.d("XMPPChatActivity", "Name: " + entry.getName());
Log.d("XMPPChatActivity", "Status: " + entry.getStatus());
Log.d("XMPPChatActivity", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());

Log.d("XMPPChatActivity", "Presence Status: "+ entryPresence.getStatus());
Log.d("XMPPChatActivity", "Presence Type: " + entryPresence.getType());

Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatActivity", "Presence AVIALABLE");
Log.d("XMPPChatActivity", "Presence : " + entryPresence);
}
} catch (XMPPException ex) {
Log.e("XMPPChatActivity", "Failed to log in as "+ USERNAME);
Log.e("XMPPChatActivity", ex.toString());
setConnection(null);
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}
}

所以基本上,我如何使它成为一项服务

最佳答案

我想给定链接中的这个示例会给您提供使其成为一项服务的想法。 http://android.codeandmagic.org/small-test-of-asmack-xmpp-client-library/

关于android - aSmack 即服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016231/

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