티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Calling startActivity() from outside of an Activity?

액티비티 외부에서 startActivity() 호출하기

 문제 내용 

I'm using an AlarmManager to trigger an intent that broadcasts a signal. The following is my code:

신호를 브로드캐스트하는 인텐트를 트리거하기 위해 AlarmManager를 사용하고 있습니다. 다음은 제 코드입니다.
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
try
{
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    Long elapsed +=  // sleep time;
    mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
}
catch(Exception r)
{
    Log.v(TAG, "RunTimeException: " + r);
}

 

I'm calling this code from an Activity, so I don't know how I could be getting the following error...

액티비티에서 이 코드를 호출하고 있으므로 다음 오류가 어떻게 발생하는지 모르겠습니다...
ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

 

 

 높은 점수를 받은 Solution 

if your android version is below Android - 6 then you need to add this line otherwise it will work above Android - 6.

만약 당신의 안드로이드 버전이 안드로이드 - 6 이하라면, 당신은 이 라인을 추가해야 합니다.
그렇지 않고 안드로이드 - 6 이상이라면 작동할 것입니다.
...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...

 

 

 가장 최근 달린 Solution 

Android Doc says -

Android Doc은 말합니다 -

 

FLAG_ACTIVITY_NEW_TASK requirement is now enforced

With Android 9, you cannot start an activity from a non-activity context unless you pass the intent flag FLAG_ACTIVITY_NEW_TASK. If you attempt to start an activity without passing this flag, the activity does not start, and the system prints a message to the log.

Note: The flag requirement has always been the intended behavior, and was enforced on versions lower than Android 7.0 (API level 24). A bug in Android 7.0 prevented the flag requirement from being enforced.

FLAG_ACTIVITY_NEW_TASK 요구 사항이 적용되었습니다.

Android 9에서는 인텐트 플래그 FLAG_ACTIVITY_NEW_TASK를 전달하지 않는 한 비 액티비티 컨텍스트에서 액티비티를 시작할 수 없습니다. 이 플래그를 전달하지 않고 액티비티를 시작하려고 하면 액티비티가 시작되지 않고 시스템이 로그에 메시지를 인쇄합니다.

참고: 플래그 요구 사항은 항상 의도된 동작이었으며 Android 7.0(API 레벨 24)보다 낮은 버전에서 시행되었습니다. Android 7.0의 버그로 인해 플래그 요구 사항이 적용되지 않았습니다.

 

That means for (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) it is mandatory to add Intent.FLAG_ACTIVITY_NEW_TASK while calling startActivity() from outside of an Activity context.

즉 (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) 액티비티 컨텍스트 외부에서 startActivity()를 호출하는 동안 Intent.FLAG_ACTIVITY_NEW_TASK를 추가해야 합니다.

 

So it is better to add flag for all the versions -

따라서 모든 버전에 플래그를 추가하는 것이 좋습니다.
...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...

 

 

출처 : https://stackoverflow.com/questions/3689581/calling-startactivity-from-outside-of-an-activity

반응형
댓글
공지사항
최근에 올라온 글