다음을 통해 공유


Xamarin.Essentials: 앱 작업

AppActions 클래스를 사용하면 앱 아이콘에서 앱 바로 가기를 만들고 응답할 수 있습니다.

시작하기

이 API를 사용하기 전에 라이브러리가 제대로 설치되고 프로젝트에 설정되어 있는지 확인하기 위해 Xamarin.Essentials에 대한 시작 가이드를 읽어보세요.

AppActions 기능에 액세스하려면 다음 플랫폼 관련 설정이 필요합니다.

MainActivity 클래스에 의도 필터를 추가합니다.

[IntentFilter(
        new[] { Xamarin.Essentials.Platform.Intent.ActionAppAction },
        Categories = new[] { Android.Content.Intent.CategoryDefault })]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ...

그런 다음 동작 처리를 위해 다음 논리를 추가합니다.

protected override void OnResume()
{
    base.OnResume();

    Xamarin.Essentials.Platform.OnResume(this);
}

protected override void OnNewIntent(Android.Content.Intent intent)
{
    base.OnNewIntent(intent);

    Xamarin.Essentials.Platform.OnNewIntent(intent);
}

작업 만들기

클래스에서 Xamarin.Essentials에 대한 참조를 추가합니다.

using Xamarin.Essentials;

앱 작업은 언제든 만들 수 있지만 애플리케이션이 시작할 때 생성되는 경우가 많습니다. SetAsync 메서드를 호출하여 앱에 대한 작업 목록을 만듭니다.

try
{
    await AppActions.SetAsync(
        new AppAction("app_info", "App Info", icon: "app_info_action_icon"),
        new AppAction("battery_info", "Battery Info"));
}
catch (FeatureNotSupportedException ex)
{
    Debug.WriteLine("App Actions not supported");
}

특정 운영 체제 버전에서 앱 작업이 지원되지 않는 경우 FeatureNotSupportedException이 throw됩니다.

AppAction에서 다음 속성을 지정할 수 있습니다.

  • ID: 작업 탭에 응답하는 데 사용되는 고유 식별자입니다.
  • 제목: 표시되어 보이는 제목입니다.
  • 부제목: 제목 아래에 표시할 하위 제목이 지원되는 경우
  • 아이콘: 각 플랫폼의 해당 리소스 디렉터리에 있는 아이콘과 일치해야 합니다.

App Actions on Homescreen

작업에 응답

애플리케이션이 OnAppAction 이벤트에 대한 등록을 시작할 때 앱 작업을 선택한 경우 선택한 작업에 따라 정보와 함께 이벤트를 보냅니다.

public App()
{
    //...
    AppActions.OnAppAction += AppActions_OnAppAction;
}

void AppActions_OnAppAction(object sender, AppActionEventArgs e)
{
    // Don't handle events fired for old application instances
    // and cleanup the old instance's event handler
    if (Application.Current != this && Application.Current is App app)
    {
        AppActions.OnAppAction -= app.AppActions_OnAppAction;
        return;
    }
    MainThread.BeginInvokeOnMainThread(async () =>
    {
        await Shell.Current.GoToAsync($"//{e.AppAction.Id}");
    });
}

GetActions

AppActions.GetAsync()를 호출하여 현재 앱 작업 목록을 가져올 수 있습니다.

API