我试图获取设备活动,例如设备在车辆、自行车、步行或跑步中旅行或不使用谷歌播放服务库旅行,但 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 服务检测设备的活动?