1

나는 ListView로 간단한 위젯을 쓰고있다. ContentObserver를 사용하여 데이터 변경 내용을 즉시 업데이트하고 싶었습니다. RemoteViewFactory 자손 클래스에 등록합니다.ContentObserver가 잠시 후 작동을 멈춘다.

@Override 
public void onCreate() { 
    Uri uri = Uri.parse("content://mms-sms/conversations"); 
    final String[] projection = new String[]{"*"}; 
    _dataCursor = _context.getContentResolver().query(uri, projection, null, null, "date DESC"); 

    if(_smsObserver == null) { 
     _smsObserver = new SmsObserver(new Handler(), _context); 
    } 

    _context.getContentResolver().registerContentObserver(uri, true, _smsObserver); 
} 

ContentObserver는 같은 클래스에 등록되지 않은 : 그것은 몇 시간 동안 잘 작동

@Override 
public void onDestroy() { 
    if(_dataCursor != null && !_dataCursor.isClosed()) 
    { 
     _dataCursor.close(); 
    } 

    if(_smsObserver != null) { 
     _context.getContentResolver().unregisterContentObserver(_smsObserver); 
    } 
} 

, mayby ​​몇 시간,하지만 밤 이후, ContentObserver 작동이 중지 내 App 위젯이 업데이트지고 있지 않습니다. OnChange 메소드에는 고급 코드가 없습니다.

@Override 
public void onChange(boolean selfChange) { 
    super.onChange(selfChange); 
    Intent smsIntent = new Intent(_context, SmsWidget.class); 
    smsIntent.setAction(SmsWidget.RELOAD_ACTION); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(_context, 0, smsIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    try { 
     pendingIntent.send(); 
    } catch (PendingIntent.CanceledException e) { 
     e.printStackTrace(); 
    } 
} 

Android 4.4.2가 설치된 기기에서 내 App 위젯을 테스트하고 있습니다.

OnEnabled() OnDisabled() 및 방법에 AppWidgetProvider 자체 RemoteViewsFactory ContentObserver에서 등록 및 등록 해제를 이동하려고. Unfortunatelly 그것이해야대로 작동하지 않았다. 약 8 시간 후 내 AppWidget이 업데이트 인 텐트를 얻지 못하게되어 ContentObserver가 등록 해제되어 있어야합니다.

답변

1

비슷한 문제에 직면했습니다. 내 해결 방법 솔루션은 AppWidgetProvider에 사용자 정의 된 의도 동작을 수신자로 브로드 캐스팅하는 것입니다.

관련 문제