7

안드로이드 7.0 (누드)이 안드로이드 6.0 (롤리팝)에 비해 인텐 트 엑스트라를 처리하는 방법에 변화가 있다면 아는 사람 있습니까?안드로이드 7 의도 불필요한 엑스트라

짧은 이야기 : 내 앱이 4.1 (16)에서 6.0 (23)의 모든 버전에서 작동하지만 Android 7.0 (24)에서 충돌합니다!

앱에서 추가 기능이있는 맞춤형 브로드 캐스트 수신기에 의도가있는 보류중인 인 텐트를 만듭니다. 그러나 안드로이드 7에서는 방송 수신자가받은 의도에 추가 정보가 없습니다. PollServerReceiver.java

Bundle extras = intent.getExtras(); 
Log.d(TAG, "onReceive: TESTING1 = " + extras.getString("TESTING1")); // null here 

// None of the three "TESTING*" keys are there! 
for (String key : extras.keySet()) { 
    Object value = extras.get(key); 
    Log.d(TAG, String.format("onReceive extra keys: %s %s (%s)", key, value.toString(), value.getClass().getName())); 
} 

스택 추적

Intent intent = new Intent(context, PollServerReceiver.class); 

// TODO: Remove after DEBUGGING is completed! 
intent.putExtra("TESTING1", "testing1"); 
intent.putExtra("TESTING2", "testing2"); 
intent.putExtra("TESTING3", "testing3"); 

// PendingIntent to be triggered when the alarm goes off. 
final PendingIntent pIntent = PendingIntent.getBroadcast(context, 
      PollServerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

// Setup alarm to schedule our service runs. 
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstRun, freqMilis, pIntent); 

MainActivity.java

분명히 충돌의 원인으로 NullPointerException이 있습니다. 모든 버전에서 충돌이 발생하는 것은 그리 이상한 일은 아니지만이 경우에는 최신 Android 만 사용할 수 있습니다. 누구든지 아이디어가 있으십니까?

참고 : (0, PendingIntent.FLAG_UPDATE_CURRENT, PendingIntent.FLAG_CANCEL_CURRENT)을 포함한 다른 플래그로 보류중인 인 텐트를 만들려고했지만 여전히 동일한 결과가 나타납니다.

+2

질문에서 코드를 사용하여 문제를 재현 할 수 없으므로 실제 코드가 질문의 코드와 일치하지 않을 수도 있습니다. 실제 코드에서, 커스텀'Parcelable' 클래스 형태의 추가 기능이 있습니까? – CommonsWare

+0

네, 3 개의 String String 엑스트라 외에 Parcelable 객체를 같은 의도로 저장하고 있습니다. 물론 Receiver에서 수신 된 의도와 Tree String Extras에도 빠져 있습니다. – Konaras

+1

24는 Parcelable 객체를 AlarmManager에 전달하는 것을 허용하지 않습니다. http://stackoverflow.com/questions/38466080/dp5-7-0-does-adding-extras-to-a-pending-intent-fail –

답변

14

ParcelablePendingIntent에 넣은 적이 특히 없었으며 it flat-out will not work in an AlarmManagerPendingIntent on Android 7.0입니다. 다른 프로세스는 Intent에 값을 채워야 할 수도 있습니다. 즉, 사용자 정의 Parcelable 클래스가 없으므로 추가 프로세스 조작과 관련된 프로세스는 사용자 프로세스가 아닌 프로세스에서 수행 할 수 없습니다.

This SO answerParcelable 자신을 byte[] /로 변환하는 형태로 해결 방법을 제공합니다.

+0

이 코드를 제공 할 수 있습니까? –

+0

직렬화 가능과 동일 – Lemberg

2

비슷한 문제가 있었지만 쉬운 해결책을 찾았습니다. 데이터를 번들에 넣고 해당 번들을 귀하의 의도와 함께 보냅니다. 제 경우에는 직렬화 가능한 객체를 내 의도로 보내려고했습니다.

설정 알람 :

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);` 
Intent intent = new Intent(context, AlarmReciever.class); 
Bundle bundle = new Bundle(); 

//creating an example object 
ExampleClass exampleObject = new ExampleClass(); 

//put the object inside the Bundle 
bundle.putSerializable("exapmle", exampleObject); 

//put the Bundle inside the intent 
intent.putExtra("bundle",bundle); 

PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

//setup the alarm 
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); 

알람을 수신은 :

public class AlarmReciever extends BroadcastReceiver { 

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

     // get the Bundle 
     Bundle bundle = intent.getBundleExtra("bundle"); 
     // get the object 
     ExampleClass exampleObject = (ExampleClass)bundle.getSerializable("example"); 
    } 

} 

그것은 나를 위해 잘 일했다. 희망이 도움 :