woong's

Android Activity 가 아닌 곳에서 Intent 하기 본문

Develop/Android

Android Activity 가 아닌 곳에서 Intent 하기

dlsdnd345 2016. 2. 13. 21:24
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android Activity 가 아닌 곳에서 Intent 하기 


E/AndroidRuntime(16053): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?


​Activity 가 아닌 곳에서 아래와 같이 Intent 를 사용하니 이와 같은 에러가 나타납니다.


Colored By Color Scripter

1
2
3
4
5
6
7
8
    /**
     * 전화번호 선택시 Direct 전화 걸기
     */
    private void selectPhoneNumber() {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(TELL_FORMAT+phoneNumber));
        context.startActivity(intent);
    }

 

 

Service , BroadcastReceiver , baseAdapter , View 등에서 Intent로 새로운 Activity를 열려고 하면 ​\

런타임 에러가 나타납니다.


안드로이드에서 View 를 사용 할때 

어디서 보여줄지 대상이 필요 합니다. 

this 또는 클래스명.this 와 같이 

어느 Task에 Activity를 띄울건지 모르겠다는 에러입니다. 


Task는 Activity를 관리하는 것으로 Activity를 띄우려면 해당 Task에 띄워야 하는데 

Activity가 아니므로 새로운 Activity를 띄우려면 에러가 나타 납니다.


Colored By Color Scripter

1
2
3
4
5
6
7
8
    /**
     * 전화번호 선택시 Direct 전화 걸기
     */
    private void selectPhoneNumber() {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(TELL_FORMAT+phoneNumber));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }



   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


​위 한줄 코드를 통해서 새로운 Task 를 통해서 띄우게 되면 에러 없이 잘 동작 하는 것 을 볼 수 있습니다 .




Comments