- Android Management API
- How it works
- API resources
- Enterprises
- Policies
- Enrollment tokens
- Devices
- Get started
- Quick Tip: How to Use Android’s Assist API
- 1. Enabling the Default Assistant
- 2. Sending Information to the Assistant
- 3. Creating a Custom Assistant
- 4. Using the Custom Assistant
- Conclusion
- How to use a web API from your Android app
- How a Web API works
- Setting up our project for Retrofit 2
Android Management API
The Android Management API is available as part of Android Enterprise, an initiative providing developers with tools to build solutions for organizations to manage their Android device fleets. The program is intended for enterprise mobility management providers (EMMs). To deploy a production solution that uses the Android Management API, EMMs need to follow the steps outlined in Release your solution.
You can use the Android Management API to support the work profile, fully managed device, and dedicated device solution sets.
See the Quickstart guide to try out the API.
How it works
The Android Management API supports the full enterprise mobility management lifecycle, from initial customer enrollment to setting up and managing devices.
As an EMM developer, you supply your customers with an on-premise or cloud-based EMM console. In your console, your customers generate device enrollment tokens and create management policies. They use the tokens to enroll devices and apply management policies to the devices they enrolled.
In the backend, your console uses the Android Management API to create enrollment tokens, policies, and other management resources. During enrollment, each device installs the API’s companion app, Android Device Policy. When policies are linked to a device in the API, Android Device Policy automatically enforces the policy settings on the device.
API resources
This section describes the primary resources used in the Android Management API.
Enterprises
An enterprises resource typically represents a single organization. You create an enterprise as part of an online setup flow that your customers use to bind their organization with your EMM solution. Policies, enrollment tokens, and devices belong to an enterprise.
Policies
The Android Management API follows a policy-driven model. A policies resource contains a group of device and app management settings that govern the behavior of a device. The range and flexibility of the settings supported in policies allow you to set up devices for a variety of different use cases.
See Create a policy for more information.
Enrollment tokens
You use enrollmentTokens to bind devices to an enterprise—a process called enrollment and provisioning. Enrollment tokens can optionally contain extra details (e.g. corporate WiFi credentials), a policyName linked to a policies resource, and a user account identifier.
After creating an enrollment token, you can pass the token to a device using one of several different provisioning methods. Devices install Android Device Policy as part of the provisioning process. If a policyName is specified in the enrollment token, then the policy will be applied immediately after provisioning is complete.
The Android Management API simplifies user management—you can enroll a device with or without specifying a user in the enrollment token.
- If you don’t specify a user, a new user will be created automatically.
- If you specify an existing user, the existing user will be associated with the device. You can associate a user with up to 10 devices.
See Provision a device for more information.
Devices
A devices resource is created when a device is successfully enrolled. The resource contains read-only details about a device, including its associated user, policy, and management mode.
Device management is carried out through policy, but you can use enterprises.devices.issueCommand to lock, reboot, or reset the password on a device. To wipe a device, call enterprises.devices.delete .
Get started
Test out the API—use the Quickstart guide to set up a device in minutes. Ensure that you understand the steps required to release your solution in a production environment before using the developer’s guide and API reference on this site to build your solution.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник
Quick Tip: How to Use Android’s Assist API
Android users today no longer have to open a browser and perform a search to learn about things they stumble upon while using an app. They can instead use an assistant. If you own a device that runs Android 6.0 or higher, you might already be familiar with its default assistant, which was initially called Google Now on Tap. Lately, its name has been changed to screen search.
Assistants, although context-sensitive, are usually not very accurate. To improve accuracy, app developers must use the Assist API. In this quick tip, I’ll introduce the basics of the API, and I’ll help you get started with creating your very own custom assistant.
1. Enabling the Default Assistant
If you’ve never used the assistant on your device or emulator, it’s probably not enabled yet. To enable it, press and hold the home button. In the dialog that pops up, press the Yes, I’m in button.
You’ll now be able to access the default assistant from any app by simply long-pressing the home button. It’s worth noting that the default assistant is a part of the Google app, and works best only after you’ve signed in to your Google account.
2. Sending Information to the Assistant
The default assistant is very powerful. It can automatically provide context-sensitive information based on the current contents of the screen. It does so by analyzing the view hierarchy of the active activity.
To see it in action, create a new activity in your Android Studio project and add the following TextView widget, which has the name of a popular novel, to its layout:
If you run your app now and long-press the home button, the default assistant will display cards that are somewhat related to the contents of the TextView widget.
By sending additional information to the assistant, you can improve its accuracy. To do so, you must first override the onProvideAssistContent() method of your Activity class.
You can now use the AssistContent object to send information to the assistant. For example, if you want the assistant to display a card that lets the user read about the novel on Goodreads, you can use the setWebUri() method.
Here’s what the new card looks like:
The Assist API also lets you pass structured data to the assistant using the setStructuredData() method, which expects a JSON-LD string. The easiest way to generate the JSON-LD string is to use the JSONObject class and its put() method.
The following sample code shows you how to generate and send structured data about the novel:
If you choose to hand-code your JSON-LD string, I suggest you make sure that it is valid using Google’s Structured Data Testing Tool.
3. Creating a Custom Assistant
If you are not satisfied with the way the Google app’s assistant handles your data, you should consider creating your own assistant. Doing so doesn’t take much effort.
All custom assistants must have the following:
- a VoiceInteractionService object
- a VoiceInteractionSession object
- a VoiceInteractionSessionService object
- an XML meta-data file describing the custom assistant
First, create a new Java class called MyAssistantSession and make it a subclass of the VoiceInteractionSession class. At this point, Android Studio should automatically generate a constructor for it.
By overriding the onHandleAssist() method of the class, you can define the behavior of your assistant. For now, let’s just make it parse the JSON-LD string we generated in the previous step and display its contents as a Toast message. As you might have guessed, to retrieve the JSON-LD string, you must use the getStructuredData() method of the AssistContent object.
The following code shows you how to display the value of the JSON-LD string’s description key as a Toast message.
A VoiceInteractionSession object must be instantiated inside a VoiceInteractionSessionService object. Therefore, create a new Java class called MyAssistantSessionService and make it a subclass of VoiceInteractionSessionService . Inside its onNewSession() method, call the constructor of MyAssistantSession .
Our assistant also needs a VoiceInteractionService object. Therefore, create one called MyAssistantService. You don’t have to write any code inside it.
To specify the configuration details of the assistant, you must create an XML metadata file and place it in the res/xml folder of your project. The file’s root element must be a tag specifying the fully qualified names of both the VoiceInteractionService and the VoiceInteractionSessionService subclasses.
Here’s a sample metadata file:
Lastly, while declaring the services in your project’s AndroidManifest.xml file, make sure that they require the BIND_VOICE_INTERACTION permission.
Your custom assistant is now ready.
4. Using the Custom Assistant
To be able to use your custom assistant, you must set it as your Android device’s default assistant. Therefore, open the Settings app and navigate to Apps > Default Apps > Assist & voice input. Next, click on the Assist app option to select your assistant.
At this point, if you run your app and long-press the home button, you should be able to see your custom assistant’s Toast message.
Conclusion
In this quick tip, you learned how to use the Assist API to interact with assistants on the Android platform. You also learned how to create a rudimentary custom assistant. But a word of caution: because assistants can read almost all the text that is present on a user’s screen, you must make sure that your custom assistant handles sensitive data in a secure manner.
To learn more about the Assist API, refer to its official documentation. And to learn more about cutting-edge coding and APIs for the Android platform, check out some of our other courses and tutorials here on Envato Tuts+!
Источник
How to use a web API from your Android app
A Web API is an online “application programming interface” that allows developers to interact with external services. These are the commands that the developer of the service has determined will be used to access certain features of their program. It is referred to as an interface because a good API should have commands that make it intuitive to interact with.
An example of this might be if we want to get information about a user from their social media account. That social media platform would likely have a web API for developers to use in order to request that data. Other commonly used APIs handle things like advertising (AdMob), machine learning (ML Kit), and cloud storage.
It’s easy to see how interacting with these types of services could extend the functionality of an app. In fact, the vast majority of successful apps on the Play Store will use at least one web API!
In this post, we’ll explore how to use a web API from within an Android app.
How a Web API works
Most APIs work using either XML or JSON. These languages allow us to send and retrieve large amounts of useful information in the form of objects.
XML is eXtensible Markup Language. If you are an Android developer, then you’re probably already familiar with XML from building your layouts and saving variables.
XML is easy to understand and generally places keys inside triangle brackets, followed by their values. It looks a bit like HTML:
JSON, on the other hand, stands for “Javascript Object Notation.” It is a short-hand for sending data online. Like XML or a CSV file, it can be used to send “value/attribute pairs.”
Here the syntax looks a little different, though:
These are “data objects” in that they are conceptual entities (people in this case) that can be described by key/value pairs. We use these in our Android apps by turning them into objects just as we normally would, with the use of classes.
To see this in action, we need to find a Web API that we can use readily. In this example, we will be using JSON Placeholder. This is a free REST API specifically for testing and prototyping, which is perfect for learning a new skill! REST is a particular architectural “style” that has become standard for communicating across networks. REST-compliant systems are referred to as “RESTful” and share certain characteristics. You don’t need to worry about that right now, however.
Setting up our project for Retrofit 2
For this example, we’ll also be using something called Retrofit 2. Retrofit 2 is an extremely useful HTTP client for Android that allows apps to connect to a Web API safely and with a lot less code on our part. This can then be used, for example, to show Tweets from Twitter, or to check the weather. It significantly reduces the amount of work we need to do to get that working.
First up, we need to add internet permission to our Android Manifest file to make sure our app is allowed to go online. Here is what you need to include:
Источник