a57
안드로이드 M 홈버튼 롱클릭 동작 - 기본 Assistant 대체하기 본문
안드로이드 M부터는 홈 버튼을 길게 눌러 구글 나우 온 탭을 이용할 수 있습니다.
또는 구글 어시스턴트를 불러올 수 있는데요,
어짜피 둘 다 사용하지 않는 유저가 많기 때문에 (특히 한국은)
이를 자신의 어시스턴트로 교체할 수 있게끔 구글에서 방법을 제공하고 있습니다.
Implementing Your Own Assistant
You may wish to implement your own assistant. As shown in Figure 2, the user can select the active assistant app. The assistant app must provide an implementation of
VoiceInteractionSessionService
andVoiceInteractionSession
as shown in thisVoiceInteraction
example. It also requires theBIND_VOICE_INTERACTION
permission. The assistant can then receive the text and view hierarchy represented as an instance of theAssistStructure
inonHandleAssist()
. It receives the screenshot throughonHandleScreenshot()
.
정말 치사하게 설명은 저게 끝이라서 ㅡ.,ㅡ
저처럼 헤매시는 분 있을까봐 오랫동안 쓰지도 않던 글을 작성합니다.
1. Assist API
Figure 3. Assist API lifecycle sequence diagram
사용자들은 설정 > 애플리케이션 > 기본 앱 > 디바이스 도우미 앱 (회사마다 번역이 다를 수도 있습니다) 에서 도우미 앱 (Assist)을 변경할 수 있다고 합니다.
Figure 2. Assist & voice input settings
Assist API
이 릴리스에서는 사용자가 도우미를 통해 앱에 참여하게 하는 새로운 방식을 제시합니다. 이 기능을 사용하려면, 사용자가 현재 컨텍스트를 사용하기 위해 도우미를 활성화해야 합니다. 일단 활성화하고 나면 Home 버튼을 길게 눌러 해당 도우미를 어느 앱에서나 불러낼 수 있습니다.
앱이 현재 컨텍스트를 도우미와 공유하지 않기로 선택하는 경우,
FLAG_SECURE
플래그를 설정하면 됩니다. 플랫폼이 도우미에게 전달하는 일반적인 일련의 정보 외에도 앱이 추가적인 정보를 공유할 수 있도록 하려면 새AssistContent
클래스를 사용할 수 있습니다.도우미에게 앱에서 가져온 추가 컨텍스트를 제공하려면, 다음 단계를 따릅니다.
Application.OnProvideAssistDataListener
인터페이스를 구현합니다.registerOnProvideAssistDataListener()
를 사용하여 이 리스너를 등록합니다.- 액티비티에 따라 각기 다른 상황별 정보를 제공하려면
onProvideAssistData()
콜백을 재정의하고, 필요한 경우 새로운onProvideAssistContent()
콜백도 재정의합니다.
2. 내 앱에 적용하기
아래 세 파일을 생성해 줍니다.
VoiceInteractionService.java
VoiceInteractionSession.javaVoiceInteractionSessionService.java
VoiceInteractionService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import android.annotation.TargetApi; import android.os.Build; import android.service.voice.VoiceInteractionService; /** * Created by alpha57 on 2017. 7. 25.. */ //Assist API 는 API23 부터 사용할 수 있습니다. @TargetApi(Build.VERSION_CODES.M) public class VoiceInteractionService extends VoiceInteractionService { @Override public void onCreate() { super.onCreate(); } } | cs |
VoiceInteractionSessionService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; import android.service.voice.VoiceInteractionSession; import android.service.voice.VoiceInteractionSessionService; /** * Created by alpha57 on 2017. 7. 25.. */ @TargetApi(Build.VERSION_CODES.M) public class VoiceInteractionSessionService extends VoiceInteractionSessionService { @Override public VoiceInteractionSession onNewSession(Bundle bundle) { return (new VoiceInteractionSession(this)); } } | cs |
VoiceInteractionSession.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import android.annotation.TargetApi; import android.app.assist.AssistContent; import android.app.assist.AssistStructure; import android.content.Context; import android.service.voice.VoiceInteractionSession; /** * Created by alpha57 on 2017. 7. 25.. */ @TargetApi(Build.VERSION_CODES.M) public class VoiceInteractionSession extends VoiceInteractionSession { public VoiceInteractionSession(Context context) { super(context); } @Override public void onHandleAssist(@Nullable Bundle data, @Nullable AssistStructure structure, @Nullable AssistContent content) { super.onHandleAssist(data, structure, content); } @Override public void onHandleScreenshot(@Nullable Bitmap screenshot) { super.onHandleScreenshot(screenshot); //스크린샷을 처리하는 부분입니다. } @Override public View onCreateContentView() { super.onCreateContentView(); View view = getLayoutInflater().inflate("your layout", null); return view; } } | cs |
AndroidManifest.xml
메니페스트에 아래 내용을 추가합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <service android:name=".VoiceInteractionService" android:permission="android.permission.BIND_VOICE_INTERACTION"> <meta-data android:name="android.voice_interaction" android:resource="@xml/assist_service"/> <intent-filter> <action android:name="android.service.voice.VoiceInteractionService"/> </intent-filter> </service> <service android:name=".VoiceInteractionSessionService" android:permission="android.permission.BIND_VOICE_INTERACTION"/> | cs |
VoiceInteractionSession의
@Overridepublic View onCreateContentView() {super.onCreateContentView();View view = getLayoutInflater().inflate("your layout", null);return view;}
이 부분에서 UI와 이벤트 처리를 해 주시면 됩니다.
@Overridepublic void onHandleScreenshot(@Nullable Bitmap screenshot) {super.onHandleScreenshot(screenshot);//스크린샷을 처리하는 부분입니다.}
@Overridepublic void onHandleAssist(@Nullable Bundle data,@Nullable AssistStructure structure,@Nullable AssistContent content) {super.onHandleAssist(data, structure, content);}
저는 이 API를 활용해서
이렇게 학교 앱의 시간표와 급식 정보를 띄워 주는 용도로 활용 했는데,
Assist API를 더 잘 활용하면 다양한 기능도 쓸 수 있을것 같습니다.
'RiDsoft > Apps' 카테고리의 다른 글
안드로이드 M 홈버튼 롱클릭 동작 - 기본 Assistant 대체하기 (0) | 2017.07.30 |
---|---|
강고 포켓 개인정보처리방침 (0) | 2017.07.18 |
춘여 피플 개인정보처리방침 (0) | 2017.05.09 |