본문 바로가기
안드로이드 코드

안드로이드 홈버튼, 뒤로가기버튼, 강제종료 코드

by 우딬 2021. 7. 29.

홈버튼 코드

    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();

	/*
		이벤트 작성
	*/
    }

 

뒤로가기 버튼

@Override
public void onBackPressed() {
    super.onBackPressed();
    
	/*
		이벤트 작성
	*/

}

 

(강제종료) 위로 올려 어플 종료

1. 안드로이드 서비스 코드

public class testService extends Service {


    @Override
    public void onTaskRemoved(Intent rootIntent) { 

        /*
            이벤트 작성
        */
        stopSelf(); // 서비스 종료
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

2. 서비스 코드 호출

public class MainActivity extends Activity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
		...
        startService(new Intent(this, testService.class));
    }
    ...
}

3. 매니패스트

<service android:name="com.example.TestService" android:stopWithTask="false" />

 

댓글