0

我试图获取设备活动,例如设备在车辆、自行车、步行或跑步中旅行或不使用谷歌播放服务库旅行,但 ActivityRecognitionResult 为空。

试用过的代码在 Class1.java 中:-

public class Class1 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent) 
{
public static String ACTION_STRING= "action";
Intent intentp = new Intent(context, Class2.class);
intentp.setAction(ACTION_STRING);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentp, PendingIntent.FLAG_ONE_SHOT);
}
}

Class2.java 中的代码:-

public class Class2 extends BroadcastReceiver 
{

 public void onReceive(Context context, Intent intent) 
 {
        if(intent.getAction() == Alarm.ACTION_STRING)
        {
            if(ActivityRecognitionResult.hasResult(intent))   //Here condition getting as false
            {
                ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
                DetectedActivity detectedActivity = result.getMostProbableActivity();
                int activityType = detectedActivity.getType();
                if(activityType != DetectedActivity.STILL)
                {
                                 //Here doing something;
                }
            }
        }
  }
}

如何使用 Google Play 服务检测设备的活动?

4

1 回答 1

0

文档中:

使用 ActivityRecognitionClient 需要 com.google.android.gms.permission.ACTIVITY_RECOGNITION 权限。

它是您的应用程序正确请求的权限吗?

于 2013-09-29T22:06:21.993 回答