基本上我有一个运行整个项目的Main类。代码工作正常但是一旦App没有聚焦它变得不活跃。我想知道如何将它作为一项服务。一个会在启动时启动。
该应用程序将成为通知的单向消息系统。即。
桌面客户端 - > Openfire Server - > Android XMPP服务 - >存储(DB) - >用于显示的Android GUI
正如我所说,守则正在运作(连接,登录,接收)但不是服务。
我可以使用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();
}
}
基本上,我如何将其作为服务
答案 0 :(得分:3)
我想在给定的链接上的这个例子会让你想到它是一个服务。 http://android.codeandmagic.org/small-test-of-asmack-xmpp-client-library/
答案 1 :(得分:1)
您需要使用Android Service Framework.
您可以查看GTalk SMS来源,因为他们使用的服务是开源的。 (主要服务是他们用来处理连接等的服务)虽然它也很复杂。
我强烈建议您查看在Android中使用服务的基本知识。
记住服务不会创建新线程,所有内容仍然在UI线程上完成,因此如果您想在后台执行长时间运行的任务,那么您还需要实现asynctask或executor服务。
答案 2 :(得分:0)
老问题,但无论如何我都会提出我的答案 您需要创建服务,启动它并将aSmack连接的代码放在服务中,而不是放在任何活动中。即使应用程序不在前台,该服务也将保留连接。我在我的一个客户应用程序中使用此方法并且它工作得很好。
此外,请确保在服务中使用Handler或AsyncTask在另一个非UI线程中创建套接字连接。 Android不允许您在UI线程中创建连接。