So first of all, we need our activity
to implement the various callbacks and
listeners that we've
been discussing about.
So, you can see here our public
class MainActivity already extends
ActionBarActivity.
We want to implement a few things.
So we want it to
implement the following.
It's going to implement
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.ConnectionFailedListe-
ner, and the location listener.
Instead of you watching me typing,
I'm just going to paste these in.
I've now added these, but
we can see we have a few errors
because some of the symbols
haven't been resolved.
You just need to import the correct
libraries to resolve them, and
I've added those libraries here.
We can now see that we still have an
error, and that's because there are some
methods that need to be implemented and
we haven't implemented them yet.
But we will be doing
that in just a moment.
Before we do that, there's a few
things that I want to add to the code.
First up, we just want to add a tag,
which is a string,
to the code that allows you to track
the name of this application if
we're writing it out to the log.
So we do that with a private
final string, it's called tag.
And you can put whatever
you like in there.
I'm just going to put LawrenceTestApp
and the code would look like this.
Next there's a few local variables that
we need to set up within this class.
Things, for example, for tracking
the text view that we call TxtOutput,
the Google API client itself,
and a location request object.
So I'll add these now.
You can see when I added them in,
it detected that it needed
to import this class.
So just go ahead and say okay to that.
And the class will be imported.
So you can see here I have a TextView
object that I'm calling txtOutput
that will just be used to
address the TextView on the UI.
I have a GoogleApiClient that
I call mGoogleApiClient.
And I have a LocationRequest
that I call mLocationRequest.
Okay, now in our onCreate method,
we need to set up the GoogleApiClient.
And we use this with
a GoogleApiClient builder, and
we add the requisite APIs to that.
Let's add this code and
then we can talk through it.
So, we go to onCreate and
I'll add the code in here.
So what it simply does is it
creates a new builder for
an API client, and it adds that we
want to use the location services API
by saying just LocationServices.API
to the addAPI method.
We want the connection callbacks on the
Google API client to come to this class.
And we want
the OnConnectionFailedListener
also to come to this class,
and then we build it.
So that's now going to give us a Google
API client that allows us to talk to
Google services.
Now, if you remember,
we had a UI element called a text view.
And this will render
the location information.
Of course, we want that text
view to be addressable in code,
using this txtOutput class.
So let's set up something
to do that right now.
And we do that like this.
We just specify that txtOutput is found
my looking for the ID, or .id.txtOutput.
If you remember, when we specified the
text view in the XML for the activity,
we called it txtOutput,
the same name as this.
We just find that view and
then we cast it into a text view, and
now we're good to go.
So just to recap,
what did we actually do here?
Well first, we created an instance of
a Google API client using its builder.
We set it to use
the LocationServices API here, and
then we told it what the callbacks for
OnConnectionFailed were this,
so we're going to be handling
them within this class, or
within this activity, and
then we hooked up to text view to code.
You've actually done a lot but
there's still more to come.
So let's have a quick quiz first.
أولاً، نحن في حاجة إلى أن يقوم النشاط
بتنفيذ استدعاءات عديدة
والمراقبات التى نحن بصددها الآن.
لذلك، يمكنك أن ترى هنا
النشاط الرئيسي العام لدينا يوسع
نشاط شريط المهام.
نريد تنفيذ أشياء قليلة.
نريده أن يطبق الآتي.
سيقوم بتنفيذ إعادة استدعاءات
عميل واجهة برمجة تطبيق Google.
موزع فشل اتصال عميل واجهة برمجة
تطبيق Google وموزع الموقع.
بدلاً من كتابتهم، سأقوم بلصقهم.
لقد أضفتهم الآن، لكن نلاحظ أن لدينا
بعض الأخطاء.
لأن هناك بعض الرموز لم يتم حلها.
لابد أن تقوم باستخدام المكتبات الصحيحة
لحل تلك الرموز
و إضافة تلك المكتبات هنا.
لايزال هناك خطأ، وذلك لأن هناك بعض
الأساليب لابد من تنفيذها ولم نقم بذلك حتى
الآن.
وسنقوم بذلك في ثواني معدودة
قبل القيام بذلك، هناك أشياء قليلة أريد
إضافتها للتعليمة البرمجية.
أولاً، نريد إضافة علامة
وهي عبارة عن سلسلة للتعليمة البرمجية حتى
تمكنك من تتبع
إسم هذا التطبيق إذا قمنا بكتابته في
التسجيل.
لذا نقوم بذلك من خلال عبارة نهائية خاصة،
تسمى علامة.
ويمكنك وضعها في أي مكان يروق لك.
سأقوم بوضع تطبيق اختبار Lawrence وقد
تبدو التعليمة البرمجية مثل ذلك.
بعد ذلك هناك متغيرات محلية لابد من إعدادها
داخل هذه الفئة.
أشياء، على سبيل المثال، لتتبع طريق عرض
النص التي نسميها مُخرَج Txt،
عميل واجهة برمجة التطبيق ذاته وعنصر طلب
الموقع.
سأقوم بذلك الآن.
يمكن أن ترى عندما قمت بإضافتهم،
تبين أنه يحتاج إلى استخدام هذه الفئة.
قم بالمتابعة والتأكيد بنعم.
وسيتم استخدام هذه الفئة.
يمكن أن تلاحظ هنا أن لدي موضوع
طريقة عرض نص سميته مُخرَج txt
التي تستخدم لكتابة طريقة عرض النص
على واجهة المستخدم
لدي عميل واجهة برمجة تطبيق Google
أسميه عميل واجهة برمجة تطبيق mGoogle.
ولدي طلب موقع أسميه طلب mLocation.
حسناً، الآن في أسلوب التكوين لدينا, لابد
أن نقوم بإعداد عميل واجهة Google.
ونستخدم ذلك من خلال مكون واجهة
برمجة تطبيق Google.
ونضيف واجهة برمجة تطبيق مشروطة لذلك.
لنقم بإضافة هذه التعليمة
و يمكن الحديث عنها باستفاضة.
نذهب إلى أسلوب التكوين وسأضيف
التعليمة البرمجية هنا.
ما تقوم به ببساطة هو إنشاء مكون جديد
لعميل واجة برمجة تطبيق، والذي يضيف أننا نريد
استخدام واجهة برمجة تطبيق خدمات الموقع
من خلال ذكر واجهة برمجة تطبيق خدمات الموقع
في أسلوب واجهة برمجة تطبيق
نريد إضافة إعادة استدعاءات عميل واجهة
برمجة تطبيق Google في هذه الفئة.
ونريد إضافة موزع فشل الاتصال
أيضاً في هذه الفئة، وتكوينها بعد ذلك.
وسيقوم بتخصيص عميل واجهة برمجة تطبيق
لنا لكي تمكننا من التحدث إلى
خدمات Google.
الآن، لو تتذكر، كان لدينا عنصر واجهة
مستخدم يسمى طريقة عرض نص.
وسيعرض ذلك بيانات الموقع.
نحتاج إلى أن تكون طريقة النص
في تعليمة برمجية
واستخدام فئة مُخرَج txt.
لنبدأ بعض الإعدادات للقيام بذلك الآن.
ونقوم بشيء مثل ذلك.
نحدد فقط أن مخرج txt موجود
ببحثي عن المعرف أو مخرج id.txt.
لو تتذكر، عندما خصصنا عرض النص
في XML للنشاط،
أطلقنا عليه اسم مخرج txt.
نجد طريقة العرض تلك ويمكننا وضعها في
طريقة عرض نص
ويمكننا أن نبدأ الآن.
وحتى نلخص، ما الذي فعلناه هنا؟
أولاً، قمنا بتكوين نموذج لعميل
واجهة برمجة تطبيق باستخدام المكون لديه.
قمنا بإعداده لاستخدام واجهة برمجة
تطبيق خدمات الموقع هنا
وأخبرناه بعد ذلك باستدعاءات فشل الاتصال،
لذا سنقوم باستخدامهم في هذه الفئة
أو داخل النشاط، وبعدها ربطنا عرض النص
بالتعليمة البرمجية.
لقد خطوت خطوات كبيرة لكن لا يزال هناك
أشياء كثيرة بعد.
لنجري هذا الاختبار السريع أولاً.
Primeiro, precisamos que nossa
atividade implemente vários callbacks
e ouvintes
dos quais temos discutidos.
Você pode ver que nossa classe
pública MainActivity já estende
ActionBarActivity.
Queremos implementar algumas coisas.
Queremos que ela
implemente o seguinte:
Vai implementar
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.ConnectionFailedListener
e o ouvinte do local.
Em vez de você ficar vendo eu digitar,
vou simplesmente colar.
Adicionei esses itens,
mas podemos ver alguns erros
pois alguns dos símbolos
não foram resolvidos.
Você simplesmente precisa importar
as bibliotecas corretas para
resolvê-los e as adicionei aqui.
Podemos ver que ainda há um erro,
e isso porque há alguns métodos
que precisam ser implementados
e ainda não os implementamos.
Mas faremos isso
em seguida.
Antes disso, há algumas coisas
que quero adicionar ao código.
Primeiro, só queremos adicionar uma tag,
que é uma string,
ao código que permite rastrear
o nome deste aplicativo
se estivermos escrevendo no log.
Fazemos isso com uma string
final privada, chamada tag.
E você pode colocar
o que quiser nela.
Vou colocar apenas LawrenceTestApp
e o código ficará assim.
Em seguida, há algumas variáveis locais
que precisamos definir nesta classe.
Coisas, por exemplo, para rastrear
textView que chamamos TxtOutput,
o próprio cliente Google API
e um objeto de solicitação de local.
Adicionarei isso agora.
Você vê que quando adicionei-os,
foi detectado que preciso
importar essa classe.
Basta continuar e dizer ok para isso.
E a classe será importada.
Você pode ver aqui que tenho um objeto
TextView que estou chamando txtOutput
que só será usado
para abordar o TextView na IU.
Tenho um GoogleApiClient
que chamo de mGoogleApiClient.
E tenho um LocationRequest
que chamo de mLocationRequest.
Ok, em nosso método onCreate,
precisamos definir o GoogleApiClient.
E usamos isso com
um compilador GoogleApiClient,
e adicionamos o requisito APIs a ele.
Vamos adicionar esse código
e poderemos conversar por ele.
Vamos para onCreate
e adiciono o código aqui.
Simplesmente
ele cria um novo compilador
para um cliente API e adiciona que
queremos usar a API de serviços locais
dizendo apenas LocationServices.API
ao método addAPI.
Queremos que os callbacks de conexão no
cliente Google API venha para essa classe.
E queremos que
OnConnectionFailedListener
também venha para essa classe,
e criamos isso.
Isso agora nos dará um cliente
Google API que nos permitirá
conversar com os serviços Google.
Se você se lembra,
tínhamos um elemento de interface chamado TextView.
E isso fornecerá
as informações de local.
Claro, queremos que aquele TextView
seja acessível em código,
usando essa classe txtOutput.
Vamos configurar algo
que faça isso agora mesmo.
E fazemos isso assim.
Especificamos que txtOutput é encontrado
procurando pelo ID ou .id.txtOutput.
Se você se lembra, quando especificamos
o TextView no XML para a atividade,
chamamos de txtOutput,
o mesmo nome que esse.
Encontramos aquela exibição
e a lançamos em um TextView,
e estamos prontos.
Apenas recapitulando,
o que de fato fazemos aqui?
Bem, primeiro criamos uma instância
de um cliente Google API usando seu compilador.
Definimos que use
a API LocationServices aqui,
e informamos quais eram os callbacks
para OnConnectionFailed,
então estaremos lidando
com eles nessa classe,
ou nessa atividade,
então conectamos com TextView para fazer o código.
Você de fato fez muito,
mas ainda há mais por vir.
Vamos fazer um rápido teste primeiro.
首先 需要让 activity 实现
我们一直在讨论的各种回调和侦听器
在这里可以看到
公共类 MainActivity 已经可扩展 ActionBarActivity
我们想要做到几件事
我们想让 activity 实现这几项功能
它将实现 GoogleApiClient.ConnectionCallbacks
GoogleApiClient.ConnectionFailedListener 和位置侦听器
与其看着我打字 不如我粘贴这些内容吧
我现在已经添加了这些东西 但是可以看到产生了几个错误
因为一些符号尚未解决
必须导入正确的库来解决它们
我在这里已经添加了这些库
现在可以看到仍然有一个错误 这是因为有一些方法需要实现
而我们尚未实现这些方法
但是很快就会实现
在实现前 我想向代码添加一些东西
首先 我们想添加一个标签
它是一个字符串 我们将其添加到代码中
该代码允许在将此应用程序写到记录中时追踪它的名称
我们使用名为标签的私有最终字符串来添加它
可以将你喜欢的任何东西放到这里
我只打算放入 LawrenceTestApp 代码看起来类似于此
接下来 需要在此类内设置几个局部变量
例如 为追踪名为 TxtOutput 的文本视图
Google API 客户端本身和位置请求对象的变量
我现在将添加它们
可以看到 当我添加它们时
它检测到需要导入此类
我只是继续进行并认可此结果
该类将被导入
可以看到这里有一个 TextView 对象 我称它为 txtOutput
该对象用于找到用户界面上的 TextView
我有一个 GoogleApiClient 名为 mGoogleApiClient
还有一个 LocationRequest 名为 mLocationRequest
现在 在 onCreate 方法中 需要设置 GoogleApiClient
我们将其与 GoogleApiClient 生成器一起使用
并向其添加必要的 API
添加此代码 然后我们可以讨论它
转到 onCreate 我将在这里添加代码
此代码用于为 API 客户端创建和添加新的生成器
我们想通过仅向 addAPI 方法传递 LocationServices.API
来使用位置服务 API
我们想让 Google API 客户端上的连接回调找到此类
我们还想让 OnConnectionFailedListener 找到此类
然后生成它
它现在将提供一个 Google API 客户端
我们可以通过该客户端与 Google 服务对话
如果你记得 我们曾有一个称为文本视图的 UI 元素
它将渲染位置信息
当然 我们希望可以在代码中找到该文本视图
使用此 txtOutput 类
现在 我们做些设置以完成此操作
我们像这样完成此操作
我们只需规定通过寻找 ID 或 .id.txtOutput 来寻找 txtOutput
如果你记得 当我们在 XML 中为 activity 指定文本视图时
我们将其称为 txtOutput 与此名称相同
我们只需找到该视图 然后将其转换为文本视图
现在我们准备好了
回顾一下 我们在这里实际上做了什么?
首先 我们使用生成器创建了一个 Google API 客户端的实例
我们设置它 以便在这里可以使用 LocationServices API
然后告诉它 OnConnectionFailed 的回调就是这个
我们打算在此类或者此 activity 内处理它们
然后将文本视图连接到代码
你已经完成了许多工作 但是还有更多的工作等你来做
我们先做一个快速测验