在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Google APIs的一个简单Java封装库:easygoogle

2017-2-7 13:38| 发布者: zhangjf| 查看: 385| 评论: 0

摘要: EasyGoogle是一个封装库简化了与Google Play Services的基础交互。这个库封装了以下APIs当前: Google Sign-In Google Cloud Messaging Google App Invites Google SmartLock for Passwords 安装 Easy ...

EasyGoogle是一个封装库简化了与Google Play Services的基础交互。这个库封装了以下APIs当前:

Google Sign-In Google Cloud Messaging Google App Invites Google SmartLock for Passwords 安装

EasyGoogle is installed by adding the following dependency to yourbuild.gradlefile:

  1. dependencies {
  2. compile 'pub.devrel:easygoogle:0.2.1+'
  3. }
复制代码
Enabling Services

Before you begin, visit this page to select Google services and add them to your Android app. Make sure to enable any services you plan to use and follow all of the steps, including modifying yourbuild.gradlefiles to enable thegoogle-servicesplugin.

Once you have agoogle-services.jsonfile in the proper place you can proceed to use EasyGoogle.

Basic

EasyGoogle makes use ofFragmentsto manage the lifecycle of theGoogleApiClient, so any Activity which uses EasyGoogle must extendFragmentActivity.

All interaction with EasyGoogle is through theGoogleclass, which is instantiated like this:

  1. public class MainActivity extends AppCompatActivity {
  2. private Google mGoogle;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mGoogle = new Google.Builder(this).build();
  8. }
  9. }
复制代码

Of course, instantiating aGoogleobject like this won't do anything at all, you need to enable features individually.

Sign-In

To enable Google Sign-In, call the appropriate method onGoogle.Builderand implement theSignIn.SignInListenerinterface:

  1. public class MainActivity extends AppCompatActivity implements
  2. SignIn.SignInListener {
  3. private Google mGoogle;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mGoogle = new Google.Builder(this)
  9. .enableSignIn(this)
  10. .build();
  11. }
  12. @Override
  13. public void onSignedIn(GoogleSignInAccount account) {
  14. // Sign in was successful.
  15. }
  16. @Override
  17. public void onSignedOut() {
  18. // Sign out was successful.
  19. }
  20. @Override
  21. public void onSignInFailed() {
  22. // Sign in failed for some reason and should not be attempted again
  23. // unless the user requests it.
  24. }
  25. }
复制代码

Then, use theSignInobject frommGoogle.getSignIn()to access API methods likeSignIn#getCurrentUser(),SignIn#signIn, andSignIn#signOut.

Cloud Messaging

To enable Cloud Messaging, you will have to implement a simpleServicein your application.

First, pick a unique permission name and make the following string resource in yourstrings.xmlfile. It is important to pick a unique name:

  1. <string name="gcm_permission">your.unique.gcm.permission.name.here</string>
复制代码

Next, add the following to yourAndroidManifest.xmlinside theapplicationtag:

  1. <!-- This allows the app to receive GCM through EasyGoogle -->
  2. <service
  3. android:name=".MessagingService"
  4. android:enabled="true"
  5. android:exported="false"
  6. android:permission="@string/gcm_permission" />
复制代码

Then implement a class calledMessagingServicethat extendsEasyMessageService. Below is one example of such a class:

  1. public class MessagingService extends EasyMessageService {
  2. @Override
  3. public void onMessageReceived(String from, Bundle data) {
  4. // If there is a running Activity that implements MessageListener, it should handle
  5. // this message.
  6. if (!forwardToListener(from, data)) {
  7. // There is no active MessageListener to get this, I should fire a notification with
  8. // a PendingIntent to an activity that can handle this.
  9. PendingIntent pendingIntent = createMessageIntent(from, data, MainActivity.class);
  10. Notification notif = new NotificationCompat.Builder(this)
  11. .setContentTitle("Message from: " + from)
  12. .setContentText(data.getString("message"))
  13. .setSmallIcon(R.mipmap.ic_launcher)
  14. .setContentIntent(pendingIntent)
  15. .setAutoCancel(true)
  16. .build();
  17. NotificationManager notificationManager =
  18. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  19. notificationManager.notify(0, notif);
  20. }
  21. }
  22. @Override
  23. public void onNewToken(String token) {
  24. // Send a registration message to the server with our new token
  25. String senderId = getString(R.string.gcm_defaultSenderId);
  26. sendRegistrationMessage(senderId, token);
  27. }
  28. }
复制代码

Note the use of the helper methodsforwardToListenerandcreateMessageIntent, which make it easier for you to either launch an Activity or create a Notification to handle the message.

TheforwardToListenermethod checks to see if there is an Activity that implementsMessaging.MessagingListenerin the foreground. If there is, it sends the GCM message to the Activity to be handled. To implementMessaging.MessagingListener, call the appropriate method onGoogle.Builderin yourActivityand implement the interface:

  1. public class MainActivity extends AppCompatActivity implements
  2. Messaging.MessagingListener {
  3. private Google mGoogle;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mGoogle = new Google.Builder(this)
  9. .enableMessaging(this, getString(R.string.gcm_defaultSenderId))
  10. .build();
  11. }
  12. @Override
  13. public void onMessageReceived(String from, Bundle message) {
  14. // GCM message received.
  15. }
  16. }
复制代码

Then, use theMessagingobject frommGoogle.getMessaging()to access API methodslikeMessaging#send.

App Invites

To enable App Invites, call the appropriate method onGoogle.Builderand implement theAppInvites.AppInviteListenerinterface:

  1. public class MainActivity extends AppCompatActivity implements
  2. AppInvites.AppInviteListener {
  3. private Google mGoogle;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mGoogle = new Google.Builder(this)
  9. .enableAppInvites(this)
  10. .build();
  11. }
  12. @Override
  13. public void onInvitationReceived(String invitationId, String deepLink) {
  14. // Invitation recieved in the app.
  15. }
  16. @Override
  17. public void onInvitationsSent(String[] ids) {
  18. // The user selected contacts and invitations sent successfully.
  19. }
  20. @Override
  21. public void onInvitationsFailed() {
  22. // The user either canceled sending invitations or they failed to
  23. // send due to some configuration error.
  24. }
  25. }
复制代码

Then, use theAppInvitesobject frommGoogle.getAppInvites()to access API methods likeAppInvites#sendInvitation.

SmartLock for Passwords

To enable Smart Lock for Passwords, call the appropriate method onGoogle.Builderand implement theSmartLock.SmartLockListenerinterface:

  1. public class MainActivity extends AppCompatActivity implements
  2. SmartLock.SmartLockListener {
  3. private Google mGoogle;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. mGoogle = new Google.Builder(this)
  9. .enableSmartLock(this)
  10. .build();
  11. }
  12. @Override
  13. public void onCredentialRetrieved(Credential credential) {
  14. // Successfully retrieved a Credential for the current device user.
  15. }
  16. @Override
  17. public void onShouldShowCredentialPicker() {
  18. // In order to retrieve a Credential, the app must show the picker dialog
  19. // using the SmartLock#showCredentialPicker() method.
  20. }
  21. @Override
  22. public void onCredentialRetrievalFailed() {
  23. // The user has no stored credentials, or the retrieval operation failed or
  24. // was canceled.
  25. }
  26. }
复制代码

Then, use theSmartLockobject frommGoogle.getSmartLock()to access API methods likeSmartLock#getCredentials()andSmartLock#save().

项目主页:http://www.open-open.com/lib/view/home/1449892875254

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 02:57

Copyright 2015-2025 djqfx

返回顶部