我已经获得了以下我要提供的这段代码。它发布用户ID,纬度和经度,它在php中更新当前用户lat和long,然后执行查询,该查询检索在您周围某个区域内的用户数组。 (检索年龄,名称..)因此,这会填充listView,但必须通过刷新或打开活动手动执行查询。但是我有一个LocationService,并且在其中有一个onLocationChanged(),它会在用户位置发生变化时更新。我需要我的查询/ asynctask每次位置更改时执行,然后在我的Activity中填充我的listView。任何建议/代码示例。
这是我的活动中调用异步任务的代码:
private void startQueryView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
LocalDatabase localDatabase = new LocalDatabase(this);
User user = localDatabase.getLoggedInUser();
String url = ServerRequests.SERVER_ADDRESS + "CombinedQuery.php";
UserFetchData task = new UserFetchData(this, user.latitude, user.longitude, user.id);
task.execute(url);
}
@Override
public void onFetchComplete(List<UserQueryData> data) {
// dismiss the progress dialog
if (dialog != null) dialog.dismiss();
// create new adapter
final UserAdapter adapter = new UserAdapter(this, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int userId = adapter.getItem(position).getId();
int i;
new DownloadImage(LocationSearch.this, userId + "_Profile").execute();
for (i = 2; i < 7; i++) {
new DownloadImage(LocationSearch.this, userId + "_Image" + Integer.toString(i)).execute();
}
ServerRequests serverRequests = new ServerRequests(LocationSearch.this);
serverRequests.fetchDifUserDataInBackground(userId, new GetDifUserCallback() {
@Override
public void done(DifUser returnedDifUser) {
if (returnedDifUser == null) {
String dir = getFilesDir().getAbsolutePath();
File file = new File(dir, userId + "_Profile.JPG");
boolean deleted = file.delete();
if (deleted) {
Log.d("THIS", "******************ProfileDeleted***********");
}
int i;
for (i = 2; i < 7; i++) {
String dir2 = getFilesDir().getAbsolutePath();
File file2 = new File(dir2, userId + "_Image" + Integer.toString(i) + ".JPG");
boolean deleted2 = file2.delete();
if (deleted2) {
Log.d("THIS", "******************Image" + Integer.toString(i) + "***********");
}
}
Toast.makeText(LocationSearch.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
} else {
Intent userIdIntent = new Intent(LocationSearch.this, UserProfileOpened.class);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("userId", userId);
editor.commit();
userIdIntent.putExtra("userId", userId);
userIdIntent.putExtra("name", returnedDifUser.name);
userIdIntent.putExtra("city", returnedDifUser.city);
userIdIntent.putExtra("age", returnedDifUser.age);
userIdIntent.putExtra("gender", returnedDifUser.gender);
userIdIntent.putExtra("places", returnedDifUser.places);
userIdIntent.putExtra("relationship", returnedDifUser.relationship);
userIdIntent.putExtra("facebook", returnedDifUser.facebook);
startActivity(userIdIntent);
}
}
});
}
});
}
@Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if (dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
如果您还需要更多内容,请说出来,我会编辑我的帖子。
答案 0 :(得分:2)
编辑:给Intent
一个镜头。
像这样进行Intent
操作:
public static String QUERY = "com.package.QUERY";
然后在您的活动中展开BroadcastReceiver
:
private class QueryReceiver extends BroadcastReceiver {
@Override
public void OnReceive(Context c, Intent i) {
startQueryView(); /*running your query when receiving an Intent*/
}
}
然后在您的活动OnCreate()
方法中,设置一个监听器:
IntentFilter filter = new IntentFilter(QUERY);
receiver = new QueryReceiver();
registerReceiver(receiver,filter); /* registering the receiver to receive only intents with the action QUERY */
最后,在位置监听器中,拍摄Intent
:
Intent i = new Intent(QUERY);
sendBroadcast(i);
您应该查看 LocationListener
的文档。
http://developer.android.com/reference/android/location/LocationListener.html
使用此功能,您可以随时了解用户的纬度/经度变化!