قالب وردپرس درنا توس
Home / IOS Development / Provides more value to users with exchange actions

Provides more value to users with exchange actions



Discs provide the ability for your app to share
information and controls with other apps. In my previous post,
Share part of your app I covered
Basics of Discs on Android. Introduced in Android Pie (API 28), they are packaged as one
library and work back to Android KitKat (API 19). If you do not already have it, read it to
Learn about the most important concepts in slices, how to make a simple and how to see them on yours
devices. Before continuing with this post, make sure you have a SliceProvider in place to build
off for the interactive pieces.

In my previous post, I covered adding more actions in Slice to do more
interactive. In this post, I will show you how to add a swap action. This kind of action is good for
Verifies that the user can enable or disable in your application. This type of control also requires you
to create a component to handle the data in your app. Read on to find out more!

Add exchange action

To exchange actions, you must create either a Service or a BroadcastReceiver to handle
updates. Intent The object that these components receive will contain additional data indicating
if the change is enabled or not. You can handle the action appropriately there. For this example
I use a Service to handle the updates.

Currently my Service will be very common. I complete the actual implementation when I finish my
Cut. This will only act as a placeholder, so I can make a valid PendingIntent .

  class    FocusService  :    IntentService   (  "FocusService"  ) [1
9659000]
] ] [ ] [Managing change of change } }

Do not forget to register your service in AndroidManifest.xml so the system can launch it
correct.

The next step is to create a path for the change. To continue with my creative strip, I want to
call this path

  / change  . 

  override    moro    onBindSlice   (  sliceUri :    Uri ):    Slice [19659010]   
      context   ]: ??.    return    null 
      return    by   (  sliceUri     lane [19659010]]   {
          "/ exchange"   - >   {
              // Show shift disc

        ] 
        ]   ->   {
            . . 
        } 
    } 
} 

To create my swap action, I can use createToggle () static method on the class SliceAction .
This method requires a PendingIntent a title and a Boolean flag indicating whether the change is
. Checked

  Board    Funny    OnBindSlice   [  SliceUri :    Uri ):    Slice     [
      Context [19659034]:    return    null 
      return    by   (.   sliceUri     lane )   {
         "/ switch"   ->   {
              // Display Swap
  ]    [19659000]]. [19659000]]. [19659000] 
            .   
         [19659000]] 
         [19659000]]    createToggleAction   () 
        } 
         Otherwise   
    ] 
] 

                 [19659000]]   =    FocusManager .   Focus 
      Val    FocusIntent   =    Intent   (  Context     FocusService   ::   Class .  . ] java ).   la   {
          PendingIntent .   getService     context    0 [19659010]       0 ) 
    } [196590101] election    toggleTitle   =    context .   getString   [  R .   string .   toggle_title ) 
      return    SliceAction [19659010]   createToggle   
              focusIntent  
              toggleTitle  
              isFocusing 
    ) 
} 

FocusManager is just an item that is responsible for determining my current status .
The data is stored in a property that can be returned quickly to my SliceProvider .

  item    FocusManager   
      public    var    focus   =    false 
} 

It is important to return this value quickly so that There is no delay when the disc is
see you later. In fact, if you take too long to return the information, an exception will be thrown and yours
Slice will not be displayed. Even reading from SharedPreferences is too long in this case. If you read
Your data takes too long, you must initialize Slice with placeholder data while you
Ask the real data in the background. Once you have the data, you can notify SliceProvider
that the data has changed and it will rebind accordingly.

When I have my toggle action, I can make the rest of the data for my Slice. I change the text
of my disc based on my shift state. If my user does not focus, I will ask them to start and
If they focus, I'll ask them to turn it off when they're done.

  override    fun    onBindSlice   (  sliceUri :    Uri  ):    Slice     
      Context   ]: ??    return    null 
      return    by   (  sliceUri     lane )   {
          "/ exchange"   - >   {
              // Display interchange
   val    toggleAction   =    createToggleAction   () 

              fall    focusTitleText :    String 
              fall    focusSubtitleText : [19659009] String 
              if   (  FocusManager .   focus )   {
                  focusTitleText   =    context .   getString   (  R .   string .   focus_title_text_enabled ) 
                  focusSubtitleText   =    context .   getString [19659010] (  R .   string .   focus_subtitle_text_enabled ) 
            }    otherwise   {
                  focusTitleText   ] =    context .   getString   (  R .   string .   focus_title_text_disabled ) 
                  focusSubtitleText   =    context [19659010].   getStri ng   (..   R     string     focus_subtitle_text_disabled ) 
            } 
        } 
         otherwise   ->   {
            . . 
        } 
    } 
} 

With my action, title and subtitle ready, I can continue to create my Slice. I can add my change
onBindSlice ( sliceUri : Uri ): ]: ]: 19659009] Slice {
context : ??. return null
return by ( sliceUri [19659018] lane ) {
"/ Press Button" - .> {
.
} otherwise {
focusTitleText = context . getString ( R . string . focus_title_text_disabled )
focusSubtitleText [19659008] = context . getString ( R . string . focus_subtitle_text_disabled )
}

list ( context ] sliceUri ListBuilder . [19659018] Infinity ) [
{
Title = focusTitleText
] subtitle = focusSubtitleText
}
addAction ( toggleAction )
}
}
otherwise -.> ]
.
}
}
}

The final step is to fill out FocusService to handle the swap changes. I can pull out
Additional data for purpose and viewed on my FocusManager . I also want to notify the system as
The contents of my Slice URI have been modified so that they can tell my SliceProvider to rebind my
Disc with updated data. "FocusService" ) {

Override Moro ] onHandleIntent : : Intent ?) {
Purposes : Return
Alternatives toggleState [19659008] = if . hasExtra ( Slice . EXTRA_TOGGLE_STATE )) {
Purpose . getBooleanExtra ( Slice . EXTRA_TOGGLE_STATE false )
} otherwise {[19659223] false
}

FocusManager . focus = toggleState

fall sliceUri = Uri . analyze ( "content: //com.bignerdranch.android.icingontheslice/toggle" [196590] ] ] ]
}

] ] With this in place, you can see . notifyChange ( sliceUri Can I run my disk and see the content change when I enable and disable

<img src = "http://www.bignerdranch.com/assets/img/blog/2018/10/toggle_off_slice.png" alt = "Sample piece showing content based on its swap disabled

    

Toggle Actions provide an easy way for users to enable or disable specific functionality in your app in a single place to find. You can either use a Service or a BroadcastReceiver to handle user input. Just configure a PendingIntent for your component and the system will start it when the data changes.

In the next post, I'll cover how to add a range action to your Slice. This allows users to provide a value from a discrete set of alternatives.

I hope you liked the post. If you have any questions, please comment below. Thanks to read!


Source link