发射后将拾音器关闭

时间:2015-10-02 17:22:28

标签: java android android-studio google-places-api

我在Android Studio上使用Google Places API来根据位置显示感兴趣的地方。我正在使用PlacePicker Inentbuilder来实现这一目标。

当应用程序运行时,位置选择器启动然后立即关闭(大约1-2秒)。

  • 我已经为Android应用程序生成了公共API密钥,并将其包含在应用程序清单中的元数据标记中。
  • 我在开发者控制台上启用了“Google Places API for android”API。
  • 我在build.gradle中包含了最新的play服务版本。

我已将我的代码和Logcat包含在下面。如果我需要包含其他任何内容,请告诉我。

的Manifest.xml:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/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" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".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>
    <activity
        android:name=".CoresActivity"
        android:label="@string/title_activity_cores" >
    </activity>
    <activity
        android:name=".MicrofoneActivity"
        android:label="@string/title_activity_microfone" >
    </activity>
    <activity
        android:name=".ConfirmaCoresActivity"
        android:label="@string/title_activity_confirma_cores" >
    </activity>
    <activity
        android:name=".BolaLocaActivity"
        android:label="@string/title_activity_bola_loca" >
    </activity>
    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" >
    </activity>
    <activity
        android:name=".PlacesAPIActivity"
        android:label="@string/title_activity_place_api" >
    </activity>
</application>

PlacesAPIActivity - 使用google places API的活动:

package com.fastcoding.appbebado;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;


public class PlacesAPIActivity extends AppCompatActivity {
    TextView getLocation;
    int PLACE_PICKER_REQUEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_api);
        getLocation = (TextView)findViewById(R.id.getLocTV);
        getLocation.setClickable(true);
        getLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                Intent intent;
                try {
                    intent = builder.build(getApplicationContext());
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);
                    Toast.makeText(PlacesAPIActivity.this,"start activity for result",Toast.LENGTH_LONG).show();
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_place_api, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("onActivityResult");
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                String toastMsg = String.format("Place: %s", place.getName());
                Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
            }
        }
    }
}

Build.gradle(app模块 - 这是唯一的模块)

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.fastcoding.appbebado"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.android.gms:play-services:7.5.0'
}

2 个答案:

答案 0 :(得分:1)

请粘贴您的logcat和manisfest文件。

可能是元数据标记应该是android:name="com.google.android.geo.API_KEY" 它应该在Manifest中的<application>标记内。

答案 1 :(得分:0)

https://console.developers.google.com/apis/dashboard

中启用 Android Places API for Android