2015-09-10 3 views
5

を、検出:これに似<code>onStartCommand()</code>と私はサービスを持っているサービスがクラッシュ

private User user; 

@Override 
public int onStartCommand(@Nullable final Intent intent, final int flags, final int startId) { 
    if (user == null) { 
     user = UserList.getInstance().getSpecialUser(); 
    } 
    Assert.assertNotNull(user); 

    // 
    // Build notification here, seomehow based on user object 
    // 
    startForeground(42, notification); 

    return START_STICKY; 
} 

を明らかに、私は私のサービスは、フォアグラウンドで実行し、できるだけ長くのために実行したいです。

これは、アプリケーションがネットワーククラスのどこかでクラッシュするまでうまくいきます。その後、アプリのクラッシュダイアログが表示され、ユーザーが確認し、アプリ全体が再作成され、サービスが再作成されますが、UserListが空になります。getSpecialUser()nullを返し、Assertは再びアプリをクラッシュします。 Androidは放棄し、ユーザーはホーム画面で終了します。

状況をより上手く処理できるクラッシュを検出する方法はありますか?

+3

デバッグでたuncaughtExceptionHandlerを作成します? –

+0

UserList.getInstance()は何を返しますか? –

+0

私はUserListの静的な言及を忘れてしまったので、UserListの "グローバル"インスタンスを返します。 – Pitel

答えて

4

ネットワーククラスのアプリケーション

public class MyApplication extends android.app.Application{ 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { 

      @Override 
      public void uncaughtException(Thread arg0, Throwable arg1) { 
       doSomething(); 
      } 
     }); 
    } 
} 
+0

それは私の仕事です。ありがとうございました –

関連する問題