Android 使用Google play服务进行活动识别和位置更新
我正在尝试根据用户的活动接收位置,即,如果用户仍然在,则位置更新的频率较低,如果他正在步行或驾驶,则更新速度较快。我编写了一个服务来接收位置更新,并在Android 使用Google play服务进行活动识别和位置更新,android,android-location,android-broadcast,Android,Android Location,Android Broadcast,我正在尝试根据用户的活动接收位置,即,如果用户仍然在,则位置更新的频率较低,如果他正在步行或驾驶,则更新速度较快。我编写了一个服务来接收位置更新,并在onCreate()方法中放置了一个广播接收器,该方法接收从主活动广播的意图。这些广播意图携带字符串,告诉我的广播接收者用户的“活动”。但此接收器从未接收到意图,因此我无法设置locationRequest计时,基于此,我将向服务传递适当的locationRequest 任何人都能告诉并帮助为什么服务的onCreate中的广播接收器可能没有被调用。
onCreate()
方法中放置了一个广播接收器,该方法接收从主活动广播的意图。这些广播意图携带字符串,告诉我的广播接收者用户的“活动”。但此接收器从未接收到意图,因此我无法设置locationRequest
计时,基于此,我将向服务传递适当的locationRequest
任何人都能告诉并帮助为什么服务的onCreate
中的广播接收器可能没有被调用。谢谢
public class MyActivityRecognition extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private ActivityRecognitionClient arclient;
private PendingIntent pIntent;
private BroadcastReceiver receiver;
private TextView tvActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvActivity = (TextView) findViewById(R.id.tvActivity);
int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resp == ConnectionResult.SUCCESS) {
arclient = new ActivityRecognitionClient(this, this, this);
arclient.connect();
} else {
Toast.makeText(this, "Please install Google Play Service.",
Toast.LENGTH_SHORT).show();
}
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String u = intent.getStringExtra("Activity");
String v = "Activity :" + intent.getStringExtra("Activity")
+ " " + "Confidence : "
+ intent.getExtras().getInt("Confidence") + "\n";
tvActivity.setText(v);
Intent activityIntent = new Intent();
intent.setAction("com.example.useractivity");
intent.putExtra("ACTIVITY", u);
sendBroadcast(activityIntent);
}
};
服务类别如下:
public class LocationService extends Service implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private static final int MILLISECONDS_PER_SECOND = 1000;
private static final int FASTEST_INTERVAL_IN_SECONDS = 5;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;
LocationRequest mLocationRequest;
LocationClient mLocationClient;
boolean mUpdatesRequested;
String mActivity="Still";
private BroadcastReceiver myReceiver;
@Override
public void onCreate() {
super.onCreate();
myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.example.useractivity")){
mActivity = intent.getExtras().getString("ACTIVITY");
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.useractivity");
registerReceiver(myReceiver, filter);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (mActivity.equalsIgnoreCase("Still")
|| mActivity.equalsIgnoreCase("Tilting")
|| mActivity.equalsIgnoreCase("Unknown")) {
mLocationRequest.setInterval(30*1000);
} else if (mActivity.equalsIgnoreCase("On Foot")
|| mActivity.equalsIgnoreCase("On Bicycle")) {
mLocationRequest.setInterval(20*1000);
} else if (mActivity.equalsIgnoreCase("In Vehicle")) {
mLocationRequest.setInterval(10*1000);
}
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationClient = new LocationClient(getApplicationContext(), this, this);
mUpdatesRequested = true;
mLocationClient.connect();
}
@Override
public void onLocationChanged(Location location) {
String latitude = Double.toString(location.getLatitude());
String longitude = Double.toString(location.getLongitude());
String msg = "Updated Location: " + latitude + "," + longitude;
Toast.makeText(getApplicationContext(), msg,
Toast.LENGTH_SHORT).show();
System.out.println(Double.toString(location.getLatitude()) + ","
+ Double.toString(location.getLongitude()));
SaveData sd = new SaveData(getApplicationContext());
sd.save(mActivity, latitude, longitude);
}
@Override
public void onConnected(Bundle arg0) {
if (mUpdatesRequested) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
}
我知道我很晚才回答这个问题,但我想我还是会回答的 在MyActivityRecognition中,您似乎正在使用传递给onReceive的“intent”变量,我认为您需要在activityIntent上设置操作和额外操作。见下文
Intent activityIntent = new Intent();
intent.setAction("com.example.useractivity");
intent.putExtra("ACTIVITY", u);
sendBroadcast(activityIntent);
也许你的意思是:
Intent activityIntent = new Intent();
activityIntent.setAction("com.example.useractivity");
activityIntent.putExtra("ACTIVITY", u);
sendBroadcast(activityIntent);
如果不使用activityIntent,您的服务将不会收到额外的信息和操作,因此不会执行您希望它执行的操作
我希望这有帮助,谢谢:)(这是我第一次尝试回答,如果正确,请将其标记为正确。)每当UserState运行时,此代码将始终调用
onLocationChanged
。您需要将布尔值设置为false,以禁用对`mlLocationClient.connect();`直到用户状态改变