在我的应用中实施Google索引

时间:2015-03-27 20:06:37

标签: java android eclipse google-index

******这是我的Android manifest.xml ******中的Activity实现

<activity
    android:name="com.zameen.zameenapp.GizmosActivity"
    android:label="@string/title_gizmos" >
    <intent-filter android:label="@string/filter_title_viewgizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/gizmos" />

        <!-- note that the leading "/" is required for pathPrefix-->
        <!-- Accepts URIs that begin with "example://gizmos”
        <data android:scheme="example"
              android:host="gizmos" />
        -->
    </intent-filter>
</activity>

这是我的活动类

package com.zameen.zameenapp;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;



import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;


import android.app.Activity;
import android.content.Intent;
import android.drm.DrmStore.Action;
import android.net.Uri;
import android.os.Bundle;

import android.util.Log;

public class GizmosActivity extends Activity
{
    static final Uri APP_URI = Uri.parse("android-app://com.zameen.zameenapp/http/www.example.com/gizmos");
    static final Uri WEB_URL = Uri.parse("http://www.example.com/gizmos/");
    private GoogleApiClient mClient;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_side_menu);
        mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.APP_INDEX_API).build();

        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();
    }

    @Override
      public void onStart() {
        super.onStart();



        // Connect your client
        mClient.connect();

        // Define a title for your current page, shown in autocompletion UI
        String title = "App Indexing API Title";

        // Construct the Action performed by the user
       Action viewAction = Action.newAction(Action.TYPE_VIEW, title, WEB_URL, APP_URI);

        // Call the App Indexing API start method after the view has completely rendered
        AppIndex.AppIndexApi.start(mClient, viewAction);


      }

      @Override
      public void onStop() {


        // Call end() and disconnect the client
        String title = "App Indexing API Title";
        Action viewAction = Action.newAction(Action.TYPE_VIEW, title, WEB_URL, APP_URI);
        AppIndex.AppIndexApi.end(mClient, viewAction);
        mClient.disconnect();


        super.onStop();
      }
     }

我正在我的应用中实施Google索引,我已经在www.example.com/gizmos的特定网址上检查了这一点,我可以使用adb命令行轻松启动我的活动。 问题是我必须导入 import com.google.android.gms.appindexing.Action; 的Action Class Library文件 但每次我尝试添加它时都会出现错误,当导入Eclipse推荐的规定库文件时,我会在 AppIndex.AppIndexApi.end newAction(Action.TYPE_VIEW, title,WEB_URL,APP_URI); 我想在我的GizmosActivity中导入Action类的库文件,可以解决这个问题。还有人可以告诉我,我的GizmosActivity代码和清单定义得很好吗?我怎样才能测试它是否运作良好 我非常感谢解决方案

2 个答案:

答案 0 :(得分:1)

我终于得到了解决方案,因为 google Services Library 未更新,使用SDK更新google Services,然后将 Sdk文件夹附加内容中的Google服务项目导入Eclipse < / strong>之后不要忘记在urs项目库中添加Google服务库。

答案 1 :(得分:0)

如果您使用的是Android Studio,可以让Gradle为您处理此问题。只需将compile 'com.google.android.gms:play-services-appindexing:8.3.0'(或任何您想要的版本)添加到您应用的build.gradle(在依赖项下),您就可以导入所需的应用索引库。

示例:

apply plugin: 'com.android.application'
    ...

    dependencies {
        compile 'com.google.android.gms:play-services-appindexing:8.3.0'
    }

Reference