识别用户在Android中的当前活动
我正在从中识别用户的当前活动 我使用以下代码创建了新的ActivityRecognitionClient:识别用户在Android中的当前活动,android,Android,我正在从中识别用户的当前活动 我使用以下代码创建了新的ActivityRecognitionClient: public class GPSLocationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener { private String TAG = "[ServiceDetect]"; // private ActivityRecognitionClient mActivity
public class GPSLocationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener {
private String TAG = "[ServiceDetect]";
//
private ActivityRecognitionClient mActivityRecognitionClient ;
private PendingIntent mPendingIntent ;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if (checkGooglePlayAvaible()) {
startTrack();
}
return START_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
// TODO Auto-generated method stub
// code detetc user acitivity here
getActivityRecognitionClient().requestActivityUpdates((2 * 60 * 1000), createPendingRequest());
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
mActivityRecognitionClient = null;
mPendingIntent.cancel();
mPendingIntent = null;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try{
connectionResult.startResolutionForResult((Activity) this.getApplicationContext(), 0);
} catch (IntentSender.SendIntentException e) {
// it happens if the resolution intent has been canceled,
// or is no longer able to execute the request.e
e.printStackTrace();
}
} else {
// Google Play services has no idea how to fix the issue
// it rarely happens for the location service
}
}
public void startTrack() {
try {
if (!getActivityRecognitionClient().isConnected() || !getActivityRecognitionClient().isConnecting() ) {
Log.v(TAG, "getActivityRecognitionClient is not connected");
getActivityRecognitionClient().connect();
}
}
public PendingIntent createPendingRequest() {
if (null != mPendingIntent) {
} else {
Intent intent = new Intent(getApplicationContext(), ServiceFour.class);
mPendingIntent = PendingIntent.getService(getApplicationContext(), 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
return mPendingIntent;
}
/**
* check googleplayservices is avaible or not
*
* @return true if is avaible flase if not
*/
public boolean checkGooglePlayAvaible() {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
return true;
}
return false;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mIsRemove = false;
}
private ActivityRecognitionClient getActivityRecognitionClient() {
if (mActivityRecognitionClient == null) {
mActivityRecognitionClient = new ActivityRecognitionClient(getApplicationContext(), this, this);
}
return mActivityRecognitionClient;
}
因此,每2分钟用户的当前活动将被发送到服务4。在我的服务中:
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
//
if (result != null) {
//
Log.d("Aha", "he he");
} else {
Log.d("Ohno", "T_T");
}
}
它工作正常,但当我卸载应用程序。然后再次安装servicefour中的所有ActivityRecognitionResult为空。只要重新启动设备,就可以正常工作。我不知道怎么解决这个问题。请帮助我,谢谢你的阅读 这是您的问题:
E/GooglePlayServicesUtil(18189): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
你的谷歌游戏服务不起作用
请确保您将它们正确地包含在您的Gradle文件等中。我将使用最新的api发布识别用户当前活动的代码
public class GPSLocationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener {
private String TAG = "[ServiceDetect]";
//
private PendingIntent mActivityRecognitionPendingIntent;
// Stores the current instantiation of the activity recognition client
private GoogleApiClient mApiClient;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if (checkGooglePlayAvaible()) {
startTrack();
}
return START_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
// TODO Auto-generated method stub
// -------------------------------------
// to remove ActivityRecognition call
// ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates
//----------------------------
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 2 * 60 * 1000,
createPendingRequest());
//
mApiClient.disconnect();
stopSelf();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
Log.d(TAG, "onConnectionFailed");
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
Log.d(TAG, "onConnectionSuspended");
mApiClient.connect();
}
public void startTrack() {
if (mApiClient == null) {
mApiClient = new GoogleApiClient.Builder(getApplicationContext()).addApi(ActivityRecognition.API)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
}
if (!mApiClient.isConnected() || !mApiClient.isConnecting()) {
mApiClient.connect();
}
}
public PendingIntent createPendingRequest() {
if (null != mActivityRecognitionPendingIntent) {
} else {
Intent intent = new Intent(getApplicationContext(), ServiceFour.class);
mActivityRecognitionPendingIntent = PendingIntent.getService(getApplicationContext(), 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
return mActivityRecognitionPendingIntent;
}
public boolean checkGooglePlayAvaible() {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
return true;
}
return false;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
现在在我的服务四。我收到ActivityRecognitionResult:
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (ActivityRecognitionResult.hasResult(intent)) {
if (bundle.containsKey("com.google.android.location.internal.EXTRA_ACTIVITY_RESULT")) {
String activityRecognitionResult = bundle
.getParcelable("com.google.android.location.internal.EXTRA_ACTIVITY_RESULT") + "";
Log.d(TAG, "activityRecognitionResult " + activityRecognitionResult +" (^.,,.^)");
}
}
有什么看起来像警告或错误的吗?logcat呢?只有这个警告:01-09 17:02:09.510:W/ResourceType18189:getEntry失败,因为entryIndex 27超出了entryCount类型3 01-09 17:02:09.510:W/ResourceType18189:W/ResourceType18189:无法在包0错误-2147483647 01-09 17:02:09.510:e/GooglePlayServicesUtil18189:Google Play服务中获取0x7f05001b t=4 e=27的条目未找到资源。检查您的项目配置以确保包含资源。01-09 17:02:28.289:W/ResourceType18189:getEntry失败,因为entryIndex 27超出了entryCount 3类型谢谢。我已经检查并更新了google play服务。但它不起作用。但是我找到了一个愚蠢的方法让它工作