2016-01-13 83 views
0

我正在使用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()); 
    } 
}; 

} 
+0

因此,運行意向服務的問題是什麼 – Mohit

+0

如果應用程序在後臺/前臺運行,沒有問題。如果我殺了應用程序,我沒有收到消息。 – MinnuKaAnae

+0

運行一個'服務',並把你的意圖在裏面... – Mohit

回答

0

基本上service在您背景即使你的應用程序被終止運行。

只需撥打服務一樣,

Intent chatService = new Intent(MainActivity.this,ChatService.class); 
startService(chatService); 
+0

我已經這樣做了,但是當應用程序被終止時,我得到了onNetworkMainThreadException()。我發現背後的原因是我在服務中使用Web服務。 – MinnuKaAnae

+0

可以發佈你的代碼以獲取更多幫助。 – 2016-01-13 12:21:01

+0

用代碼編輯我的問題。請檢查一次 – MinnuKaAnae

0

你應該startService和bindService 如果您只使用bindService,它將一旦你離開類喪生。 StartService將保持您的服務活着。
如下:

Intent intentService = new Intent(MainActivity.this, MyService.class); 
startService(intentService); 
bindService(new Intent(MainActivity.this, MyService.class),MainActivity.this, BIND_AUTO_CREATE); 
1

您可以添加報警火每隔X時間,這個報警觸發服務。解決方案1:警報 - 呼叫 - > BroadcastReceiver - 消防(開始) - >您的服務。解決方案2:任何系統BroadcastReceiver(例如電池電量變化) - >您的服務。

-----更新-------

public class BaseNotificationManager extends BroadcastReceiver { 


public static final String BaseAction = "Com.TST.BaseAction"; 
public static final String FireService = "Com.TST.FireNotificationAction"; 


private static final long timeFrame = 1000*60*5; // 5 mints 

public BaseNotificationManager() { 
} 

@Override 
public void onReceive(Context context, Intent intent) { 

    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 
     /// add base alarm manager 
     startBaseAlarmManager(context); 

    }else if (BaseAction.equals(intent.getAction())){ 
     // StartYourService(); 
    } 
}  public static void startBaseAlarmManager (Context context){ 


    AlarmManager alarmMgr; 
    PendingIntent alarmIntent; 

    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, BaseNotificationManager.class); 
    intent.setAction(BaseAction); 
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      5000, timeFrame, alarmIntent); 

}} 

清單

<receiver 
      android:name=".BaseNotificationManager" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
+0

您可以發佈任何相關鏈接或代碼 – MinnuKaAnae

+0

檢查更新。 –

+0

非常感謝你的代碼。我會嘗試這一個 – MinnuKaAnae

0

嘗試在onStartCommand添加startForeground,它會提高你的業務的優先級。但它應該伴隨着通知的存在。

startForeground(int id, Notification notification) 
+0

我使用IntentService,所以如果我殺了應用程序OnStartCommand()將不會執行。那麼,我該如何解決這個問題 – MinnuKaAnae

+0

當然,在你殺死你的應用程序之前,你必須新建並開始你的服務。然後,一旦你啓動服務,它會OnStartCommand()。 –

+0

或嘗試返回Service.START_STICKY; –

相關問題