Nano Hash - криптовалюты, майнинг, программирование

Эмулятор Android: это приложение не будет работать без сервисов Google Play

Начиная с Android 4.2.2 можно запускать службы Google на эмуляторе Android. В настоящее время я делаю приложение для Android и создал тестовый проект, чтобы проверить, могу ли я заставить работать вход и выход из Google+.

Я следовал следующему руководству: http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

С дополнительной информацией, используемой из следующих руководств/сайтов:

Это сгенерировало следующий код:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testproject_gmaillogin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.example.testproject_gmaillogin.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

строки.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestProject_GmailLogin</string>
    <string name="action_settings">Settings</string>

    <string name="profile_pic_description">Google Profile Picture</string>
    <string name="btn_logout_from_google">Logout from Google</string>
    <string name="btn_revoke_access">Revoke Access</string>

</resources>

Activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/profile_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal"
        android:weightSum="3"
        android:visibility="gone">

        <ImageView
            android:id="@+id/img_profile_pic"
            android:contentDescription="@string/profile_pic_description"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_weight="2" >

            <TextView
                android:id="@+id/txt_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/txt_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

    <com.google.android.gms.common.SignInButton
        android:id="@+id/btn_sign_in"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"/>

    <Button
        android:id="@+id/btn_sign_out"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_logout_from_google"
        android:visibility="gone"
        android:layout_marginBottom="10dp"/>

    <Button
        android:id="@+id/btn_revoke_access"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_revoke_access"
        android:visibility="gone" />

</LinearLayout>

Основная активность.java:

package com.example.testproject_gmaillogin;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements ConnectionCallbacks, OnConnectionFailedListener, OnClickListener
{
    // Logcat tag
    private static final String TAG = "MainActivity";

    // Profile pix image size in pixels
    private static final int PROFILE_PIC_SIZE = 400;

    // Request code used to invoke sign in user interactions
    private static final int RC_SIGN_IN = 0;

    // Client used to interact with Google APIs
    private GoogleApiClient mGoogleApiClient;

    // A flag indicating that a PendingIntent is in progress and prevents
    // us from starting further intents
    private boolean mIntentInProgress;

    // Track whether the sign-in button has been clicked so that we know to resolve
    // all issues preventing sign-in without waiting
    private boolean mSignInClicked;

    // Store the connection result from onConnectionFailed callbacks so that we can
    // resolve them when the user clicks sign-in
    private ConnectionResult mConnectionResult;

    // The used UI-elements
    private SignInButton btnSignIn;
    private Button btnSignOut, btnRevokeAccess;
    private ImageView imgProfilePic;
    private TextView txtName, txtEmail;
    private LinearLayout profileLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the UI-elements
        btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
        btnSignOut = (Button) findViewById(R.id.btn_sign_out);
        btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
        imgProfilePic = (ImageView) findViewById(R.id.img_profile_pic);
        txtName = (TextView) findViewById(R.id.txt_name);
        txtEmail = (TextView) findViewById(R.id.txt_email);
        profileLayout = (LinearLayout) findViewById(R.id.profile_layout);

        // Set the Button onClick-listeners
        btnSignIn.setOnClickListener(this);
        btnSignOut.setOnClickListener(this);
        btnRevokeAccess.setOnClickListener(this);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
    }

    @Override
    protected void onStart(){
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop(){
        super.onStop();

        if(mGoogleApiClient.isConnected())
            mGoogleApiClient.disconnect();
    }

    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.btn_sign_in:
                signInWithGPlus();
                break;
            case R.id.btn_sign_out:
                signOutFromGPlus();
                break;
            case R.id.btn_revoke_access:
                revokeGPlusAccess();
                break;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if(!result.hasResolution()){
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }

        if(!mIntentInProgress){
            // Store the ConnectionResult so that we can use it later when the user clicks 'sign-in'
            mConnectionResult = result;

            if(mSignInClicked)
                // The user has already clicked 'sign-in' so we attempt to resolve all
                // errors until the user is signed in, or they cancel
                resolveSignInErrors();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent){
        if(requestCode == RC_SIGN_IN && responseCode == RESULT_OK)
            SignInClicked = true;

            mIntentInProgress = false;

            if(!mGoogleApiClient.isConnecting())
                mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        // Get all the user's information
        getProfileInformation();

        // Update the UI after sign-in
        updateUI(true);
    }

    @Override
    public void onConnectionSuspended(int cause){
        mGoogleApiClient.connect();
        updateUI(false);
    }

    // Updating the UI, showing/hiding buttons and profile layout
    private void updateUI(boolean isSignedIn){
        if(isSignedIn){
            btnSignIn.setVisibility(View.GONE);
            btnSignOut.setVisibility(View.VISIBLE);
            btnRevokeAccess.setVisibility(View.VISIBLE);
            profileLayout.setVisibility(View.VISIBLE);
        }
        else{
            btnSignIn.setVisibility(View.VISIBLE);
            btnSignOut.setVisibility(View.GONE);
            btnRevokeAccess.setVisibility(View.GONE);
            profileLayout.setVisibility(View.GONE);
        }
    }

    // Sign-in into Google
    private void signInWithGPlus(){
        if(!mGoogleApiClient.isConnecting()){
            mSignInClicked = true;
            resolveSignInErrors();
        }
    }

    // Method to resolve any sign-in errors
    private void resolveSignInErrors(){
        if(mConnectionResult.hasResolution()){
            try{
                mIntentInProgress = true;

                //Toast.makeText(this, "Resolving Sign-in Errors", Toast.LENGTH_SHORT).show();

                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            }
            catch(SendIntentException e){
                // The intent was cancelled before it was sent. Return to the default
                // state and attempt to connect to get an updated ConnectionResult
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    // Fetching the user's infromation name, email, profile pic
    private void getProfileInformation(){
        try{
            if(Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null){
                Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String personEmail = Plus.AccountApi.getAccountName(mGoogleApiClient);

                Log.e(TAG, "Name: " + personName + ", "
                        + "plusProfile: " + personGooglePlusProfile + ", "
                        + "email: " + personEmail + ", "
                        + "image: " + personPhotoUrl);

                txtName.setText(personName);
                txtEmail.setText(personEmail);

                // by default the profile url gives 50x50 px image,
                // but we can replace the value with whatever dimension we
                // want by replacing sz=X
                personPhotoUrl = personPhotoUrl.substring(0, personPhotoUrl.length() - 2)
                        + PROFILE_PIC_SIZE;

                new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);
            }
            else{
                Toast.makeText(getApplicationContext(), "Person information is null", Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    // Sign-out from Google
    private void signOutFromGPlus(){
        if(mGoogleApiClient.isConnected()){
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            updateUI(false);
        }
    }

    // Revoking access from Google
    private void revokeGPlusAccess(){
        if(mGoogleApiClient.isConnected()){
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                .setResultCallback(new ResultCallback<Status>(){
                    @Override
                    public void onResult(Status s){
                        Log.e(TAG, "User access revoked!");
                        mGoogleApiClient.connect();
                        updateUI(false);
                    }
                });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings)
            return true;

        return super.onOptionsItemSelected(item);
    }
}

LoadProfileImage.java:

package com.example.testproject_gmaillogin;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

/**
 * Background async task to load user profile picture from url
 **/
public class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
    private ImageView bmImage;

    public LoadProfileImage(ImageView bmImage){
        this.bmImage = bmImage;
    }

    @Override
    protected Bitmap doInBackground(String... urls){
        String urlDisplay = urls[0];
        Bitmap mIcon11 = null;
        try{
            InputStream in = new java.net.URL(urlDisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        }
        catch(Exception ex){
            Log.e("Error", ex.getMessage());
            ex.printStackTrace();
        }
        return mIcon11;
    }

    @Override
    protected void onPostExecute(Bitmap result){
        bmImage.setImageBitmap(result);
    }
}

Другие шаги, которые я сделал, были:

В https://console.developers.google.com/project я создал проект с:

API Google+ на:

Google+ API включен

И идентификатор клиента, созданный с правильным SHA1 и точно таким же пространством имен, что и проект:

И идентификатор клиента, созданный с правильным SHA1

На затмении:

Я установил библиотеку google-play-services:

Установлены службы Google Play

И добавил его в проект:

Добавлена ​​библиотека сервисов Google Play (2)Добавлена ​​библиотека сервисов Google Play (2)

Я также создал эмулятор версии 4.4.2:

Я также создал эмулятор версии 4.4.2

Но когда я запускаю приложение, я получаю следующую ошибку, и эта ошибка продолжает появляться, когда я нажимаю кнопку:

Получить ошибку сервисов Google Play

Кто-нибудь знает, где что-то идет не так? Заранее спасибо за ответы.


Ответы:


1

Хорошо, попробовав кое-что, выяснилось, что у меня был неправильный выбор последнего варианта, который нигде не упоминался в учебнике (ах).

Вместо Android 4.4.2 в качестве цели сборки проекта и цели эмулятора я выбрал API Google 4.4.2. Теперь я больше не получаю ошибку сервисов Google Play.

Хотя я получаю исключение NullPointerException, но, по крайней мере, я могу продолжать.. ;)

РЕДАКТИРОВАТЬ: исправлено исключение NullPointerException и изменен код в моем исходном сообщении. Теперь все работает как надо, и я надеюсь, что этот пост поможет другим людям с такими же (или другими) ошибками при входе в сервисы Google Play с помощью эмулятора Android.

Ошибка решенияЭмулятор ошибки решения

24.04.2014
  • Не могли бы вы объяснить, как перейти к экрану Project Build Target, чтобы я мог убедиться, что мой проект использует библиотеку сервисов Google Play? 25.10.2015
  • @TonyWickham У меня нет Eclipse на моем новом ноутбуке, поэтому я не могу проверить, но я полагаю, что это был щелчок правой кнопкой мыши по проекту -> Свойства -> вкладка Android слева. 26.10.2015
  • Ах, вы использовали Eclipse - я использую Android Studio, что объясняет, почему он выглядит по-другому. 26.10.2015

  • 2

    Попробуйте использовать API-интерфейс Goole API уровня 19 в качестве цели эмулятора вместо обычного уровня API 19.

    24.04.2014
  • Когда я делал скриншоты вопроса, я пытался изменить Android 4.4.2 на Google API 4.4.2 в Build Target, и это действительно работает. (Я только что закончил свой вопрос, чтобы помочь другим людям с такими же проблемами, так как я видел довольно много сообщений с этой ошибкой.) 24.04.2014
  • Я тоже сталкивался с этим несколько раз, поэтому даже не прочитал весь вопрос и ответил сразу после того, как увидел имя цели на скриншоте эмулятора». 24.04.2014
  • Вы спасли мой день. Интересно, почему эта информация плохо документирована. 21.11.2014
  • Фантастика, спасибо за исправление. Пришлось изменить мой эмулятор, чтобы использовать API Google. Работает в настоящее время. 24.11.2014
  • Новые материалы

    Кластеризация: более глубокий взгляд
    Кластеризация — это метод обучения без учителя, в котором мы пытаемся найти группы в наборе данных на основе некоторых известных или неизвестных свойств, которые могут существовать. Независимо от..

    Как написать эффективное резюме
    Предложения по дизайну и макету, чтобы представить себя профессионально Вам не позвонили на собеседование после того, как вы несколько раз подали заявку на работу своей мечты? У вас может..

    Частный метод Python: улучшение инкапсуляции и безопасности
    Введение Python — универсальный и мощный язык программирования, известный своей простотой и удобством использования. Одной из ключевых особенностей, отличающих Python от других языков, является..

    Как я автоматизирую тестирование с помощью Jest
    Шутка для победы, когда дело касается автоматизации тестирования Одной очень важной частью разработки программного обеспечения является автоматизация тестирования, поскольку она создает..

    Работа с векторными символическими архитектурами, часть 4 (искусственный интеллект)
    Hyperseed: неконтролируемое обучение с векторными символическими архитектурами (arXiv) Автор: Евгений Осипов , Сачин Кахавала , Диланта Хапутантри , Тимал Кемпития , Дасвин Де Сильва ,..

    Понимание расстояния Вассерштейна: мощная метрика в машинном обучении
    В обширной области машинного обучения часто возникает необходимость сравнивать и измерять различия между распределениями вероятностей. Традиционные метрики расстояния, такие как евклидово..

    Обеспечение масштабируемости LLM: облачный анализ с помощью AWS Fargate и Copilot
    В динамичной области искусственного интеллекта все большее распространение получают модели больших языков (LLM). Они жизненно важны для различных приложений, таких как интеллектуальные..