aSmack作为服务 [英] aSmack as a service

查看:486
本文介绍了aSmack作为服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个主类运行整个项目。在code完美地工作,尽管一旦应用程序是没有重点就变成无效。我想知道我怎么会去使它的服务。一个会启动时启动。

Basically I have a Main class running the entire project. The code is working perfectly although once the App is unfocused it becomes inactive. I was wondering how I would go about making it a service. One that would startup at boot.

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

The app will be a one way message system for notifications. I.E.

桌面客户端 - > Openfire的服务器 - > Android的XMPP服务 - >存储(DB) - > Android的图形用户界面的显示

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

我已经说过了,在code是工作(连接,登录,接收),但不是一种服务。

As I've said, the Code is working (Connect, Login, Receive) but isn't a service.

我可以使用BEEM源,但它太特色的扫描和隔行扫描。我一个轻量级的售后服务是。

I could use the BEEM source but it's too featured and interlaced. I'm after a lightweight service.

在code:

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();
  }
}

因此​​,基本上,我该如何使这个服务

So basically, How do I make this a service

推荐答案

我想这个例子中,在给定的链接会给你的想法使之成为服务。 HTTP://android.$c$candmagic。组织/小测试 - asmack的-XMPP客户端库/

i guess this example at the given link would give you the idea for making it a service. http://android.codeandmagic.org/small-test-of-asmack-xmpp-client-library/

这篇关于aSmack作为服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆