我正在使用IntentService
实施基于聊天的应用程序。现在我需要获得消息,即使应用程序不在后台。很明显,我想要的是我需要运行意向服务,如果应用程序不处于后台状态。如何运行意向服务甚至应用程序被杀害/销毁
public class ChatService extends IntentService {
JabberSmackAPI c = null;
XMPPConnection connection;
@Override
public void onCreate() {
super.onCreate();
System.out.println("chatservice------onCreate-----");
}
@Override
public int onHandleIntent(Intent arg0) {
System.out.println("--chat services--");
try {
setUp(c);
} catch (XMPPException e) {
// TODO Auto-generated catch block
System.out.println("--XMPPConnection error--chat service--");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("--IOException error--chat service--");
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void setUp(JabberSmackAPI c) throws XMPPException, IOException {
// declare variables
c = new JabberSmackAPI();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg = "hi how r u!";
// Enter your login information here
c.login(chat_id, GlobalDeclaration.paswword);
System.out.println("-----");
String talkTo = "username";
}
public class JabberSmackAPI implements MessageListener, ChatManagerListener {
public void login(String userName, String password) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("Host", 5222);
connection = new XMPPConnection(config);
connection.connect();
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(packetListener, filter);
connection.login(userName, password);
}
}
private PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
System.out.println("In the process packet");
System.out.println(packet.toXML());
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("FILE_DOWNLOADED_ACTION");
getBaseContext().sendBroadcast(broadcastIntent);
System.out.println("---final packrt result-- " + packectResult.trim());
}
};
}
因此,运行意向服务的问题是什么 – Mohit
如果应用程序在后台/前台运行,没有问题。如果我杀了应用程序,我没有收到消息。 – MinnuKaAnae
运行一个'服务',并把你的意图在里面... – Mohit