首次启动应用程序时,Android服务不会创建通知或broadbast意图
这是我的服务课。当我第一次启动应用程序时,服务会被创建,但在我关闭应用程序之前,它不会显示任何通知。当我关闭应用程序时,服务将重新启动,并开始向我发送通知和广播首次启动应用程序时,Android服务不会创建通知或broadbast意图,android,service,notifications,xmpp,broadcast,Android,Service,Notifications,Xmpp,Broadcast,这是我的服务课。当我第一次启动应用程序时,服务会被创建,但在我关闭应用程序之前,它不会显示任何通知。当我关闭应用程序时,服务将重新启动,并开始向我发送通知和广播 public class XMPPService extends Service { private XMPPTCPConnection mConnection = null; private ChatManager chatManager; int mNotificationId = 1; @Override public int
public class XMPPService extends Service {
private XMPPTCPConnection mConnection = null;
private ChatManager chatManager;
int mNotificationId = 1;
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
try {
onHandleIntent(intent);
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
}
}).start();
return START_STICKY;
}
protected void onHandleIntent(Intent intent) throws IOException, XMPPException, SmackException {
if (mConnection == null) {
mConnection = ProfileCreationActivity.returnConnection();
if(!mConnection.isAuthenticated())
mConnection.login(new TinyDB(getApplicationContext()).getString("username"), new TinyDB(getApplicationContext()).getString("password"));
chatManager = ChatManager.getInstanceFor(mConnection);
chatManager.addChatListener(new ChatManagerListenerImpl());
StanzaFilter filter = new StanzaTypeFilter(Message.class);
mConnection.addSyncStanzaListener(new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Message message = (Message) packet;
}
}, filter);
}
mConnection.addConnectionListener(new AbstractConnectionListener() {
public void connectionClosed() {
Log.i("connection", "closed");
}
public void connectionClosedOnError(Exception e) {
Log.i("connection", "closed on error");
}
public void reconnectionFailed(Exception e) {
Log.i("reconnection", "failed");
}
public void reconnectionSuccessful() {
if (mConnection.isAuthenticated()) {
Log.i("isAuthenticated", String.valueOf(XMPPClient.getConnection().isAuthenticated()));
} else {
try {
mConnection.login(new TinyDB(getApplicationContext()).getString("username"), new TinyDB(getApplicationContext()).getString("password"));
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("reconnection", "succesful");
}
public void reconnectingIn(int seconds) {
Log.i("reconnectingIn", String.valueOf(seconds));
}
});
}
@Override
public void onCreate() {
Log.i("service", "created");
}
private class ChatManagerListenerImpl implements ChatManagerListener {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
if (!createdLocally) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.i("yeah :", "yeah");
new DBHelper(getApplicationContext()).addMessage(message.getBody().toString(), message.getFrom().toString().substring(0, 12), false);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.user);
builder.setContentText(message.getBody().toString());
builder.setContentTitle(message.getFrom().toString().substring(0, 12));
builder.setPriority(Notification.PRIORITY_HIGH);
builder.setDefaults(Notification.DEFAULT_ALL);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, builder.build());
Intent intent = new Intent();
intent.setAction("com.SINGLE_MESSAGE_RECEIVED");
sendBroadcast(intent);
Log.i("Notification : ", "notified");
}
});
}
}
}
@Override
public void onDestroy() {
mConnection.disconnect();
Log.i("service :", "destroyed");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
在检查与xmpp服务器的连接时,我正在主活动中启动服务。代码如下:
private class CheckConnection extends AsyncTask<Void, Void, XMPPTCPConnection> {
@Override
protected XMPPTCPConnection doInBackground(Void... params) {
if (!XMPPClient.getConnection().isAuthenticated()) {
try {
XMPPClient.getConnection().login(new TinyDB(getApplicationContext()).getString("username"), new TinyDB(getApplicationContext()).getString("password"));
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return XMPPClient.getConnection();
} else
{
return XMPPClient.getConnection();
}
}
@Override
protected void onPostExecute(XMPPTCPConnection con) {
if (isMyServiceRunning(XMPPService.class) && XMPPClient.getConnection().isAuthenticated()) {
Log.i("Service is :", "Running");
getForms(con);
} else {
Intent serviceIntent = new Intent(MainActivity.this, XMPPService.class);
startService(serviceIntent);
if (XMPPClient.getConnection().isAuthenticated()) {
getForms(con);
}
}
}
}
显示您的AndroidManifesti我使用的是服务而不是意图服务,所以它不是我自己必须调用的覆盖