본문 바로가기

MOBILE/android

[Android] intent가 왜 이럴까

특정 시간 알림을 설정하는데 내 코드 구조는

1. intent에 알림 정보를 담아서 pendingintent에 넣음

2. 그걸 alarmManager.set() 로 설정함

3. alarmmanager 는 broadcast receiver를 상속받아서 구현

4. 거기에 전달된 intent에 (알림 정보 담겨있는 intent) string extra 정보를 꺼내서 다시 intent에 넣고 

5. 서비스로 보내서 서비스에서 알림을 처리함

 

간단하게

MainActivity => BroadcastReceiver => Service

 

근데 이때  1번과 4번에서 사용한 인텐트 모두 명시적 인텐트를 사용했음

 

// 1번: 알림 매니저 설정하는 코드
Intent alarmIntent=new Intent(MainActivity.this,AlarmReceiver.class);


// 2번: 브로드 캐스트 리시버
Intent serviceIntent=new Intent(context,AstroAlarmService.class);

 

 

근데 이랬더니 브로드 캐스트 리시버의 onReceive로 인텐트가 안넘어가는 이슈가 발생했다.

 

@Override
public void onReceive(Context context, Intent intent) {
   
   // 여기서 계속 이슈
    Log.d("alarmtest",intent.getStringExtra("title"));
    Log.d("alarmtest",intent.getStringExtra("content"));

    Intent serviceIntent=new Intent(context,AstroAlarmService.class);

    serviceIntent.putExtra("title",intent.getStringExtra("title"));
    serviceIntent.putExtra("content",intent.getStringExtra("content"));

    context.startService(serviceIntent);
}

 

왜 이럴까?

그래서 혹시나 해서 암시적 인텐트로 필터를 설정해줬음

 

// 매니페스트 파일
<service
    android:name=".AstroAlarmService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.astro.ALARM_RECEIVER"/>
    </intent-filter>
</service>

근데 이랬더니 해결됨

왜지? 아직 원인을 모름

 

일단 이렇게 명시적 인텐트로 전달이 안될 경우에는 암시적 인텐트를 추가해주는 것으로 해결할 수 있다고 알고 있자

시험 끝나면 알아봐야해...