2014-03-03 50 views
0

startService未啓動該服務。我從應該是應用程序上下文中調用它,但是儘管從startService之前獲得了控制檯日誌消息,但是我沒有從服務中獲得任何一個。startService在應用程序上下文中調用時未啓動

public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback { 

private static final String TAG = "im.tox.antox.TAG"; 
public static final String FRIEND_KEY = "im.tox.antox.FRIEND_KEY"; 
public static final String FRIEND_MESSAGE = "im.tox.antox.FRIEND_MESSAGE"; 

private Context ctx; 

public AntoxOnFriendRequestCallback(Context ctx) { 
    this.ctx = ctx; 
} 

@Override 
public void execute(String publicKey, String message){ 
    Log.d(TAG, "Friend request callback"); 
    Intent intent = new Intent(this.ctx, ToxService.class); 
    intent.setAction(Constants.FRIEND_REQUEST); 
    intent.putExtra(FRIEND_KEY, publicKey); 
    intent.putExtra(FRIEND_MESSAGE, message); 
    this.ctx.startService(intent); 
} 
} 

這裏是要點:https://gist.github.com/ollieh/ed93a647430645fd2ee0

AntoxFriendRequestCallback在線61中ToxService

調用getApplicationContext()

我在AntoxFriendRequestCallback

看到 「好友請求回調」 在日誌中從管線15

我在ToxService的第140行日誌中看不到「Constants.FRIEND_REQUEST」,或者在MainActivity的第20行中顯示「test」。

如果你想看到完整的文件,他們在這裏: https://github.com/ollieh/Antox/tree/83eb974589a4664b2098bc0561fd0060960cfe22/app/src/main/java/im/tox/antox

+0

請確保您的清單中有服務條目。 –

+0

忘了提及它在那裏。此外,該服務在代碼中的其他地方成功啓動。 – ollieh

回答

0

發現問題。 startService在應用程序的前面用DO_TOX intent調用,並且在處理這個事件的服務位中,有一個無限循環用於重複地調用某些東西,但是這阻止了所有新的startService(),因爲它們得到排隊,需要最後完成。我用ScheduledExecuterService取代了無限循環,現在它可以工作。

0

您必須確保服務是否宣佈 「AndroidManifest.xml中」 像

<service 
     android:name="[your service class]" 
     android:enabled="true" 
     android:icon="@drawable/ic_launcher" > 
    </service> 

和 意向意圖=新意圖(this.ctx,ToxService.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // add this line intent.setAction(Constants.FRIEND_REQUEST); intent.putExtra(FRIEND_KEY,publicKey); intent.putExtra(FRIEND_MESSAGE,message); this.ctx.startService(intent);

相關問題