******这是我在Android清单中的活动实现.xml******
<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();
}
}
我正在我的应用程序中实现谷歌索引,我已经检查了在特定的Url www.example.com/gizmos上,我可以使用adb命令行轻松地开始我的活动。问题是,我必须导入操作类库文件,该文件导入com.google.android.gms.appindexing.Action但每次我尝试添加它时,我都会收到错误,当按照Eclipse的建议导入指定的库文件时,我会在AppIndex.AppIndexApi.end和newAction(Action.TYPE_VIEW,title,WEB_URL,APP_URI)中收到错误我想在我的GizmosActivity中导入Action类的库文件,这可以解决问题。还有人可以告诉我,我的GidsmosActivity代码和清单定义得很好吗?我该如何测试它是否正常工作我非常感谢
我终于得到了解决方案,这是因为谷歌服务库没有更新,使用SDK更新谷歌服务,然后将谷歌服务项目从SDK文件夹临时导入到您的Eclipse中,之后别忘了在urs项目库中添加谷歌服务库。
如果你使用的是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'
}
参考