我已经得到了下面将提供的这段代码。它发布用户 ID、纬度和经度,在 php 中更新当前用户的纬度和经度,然后执行一个查询,检索您周围某个区域内的用户数组。 (检索年龄、姓名..)因此,这会填充一个 listView,但必须通过刷新或打开 Activity 来手动执行查询。不过,我有一个 LocationService,其中有一个 onLocationChanged() ,它会在用户位置更改时更新。我需要我的查询/异步任务在每次位置更改时执行,然后在我的 Activity 中填充我的 ListView 。任何建议/代码示例。
这是我的 Activity 中调用异步任务的代码:
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();
}
如果您需要更多信息,请说出来,我会编辑我的帖子。
最佳答案
编辑:尝试一下 Intent
。
创建一个 Intent
操作,如下所示:
public static String QUERY = "com.package.QUERY";
然后在您的 Activity 中扩展BroadcastReceiver
:
private class QueryReceiver extends BroadcastReceiver {
@Override
public void OnReceive(Context c, Intent i) {
startQueryView(); /*running your query when receiving an Intent*/
}
}
然后在您的 Activity 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
使用此功能,您将知道用户的纬度/经度何时发生变化!
关于php - 填充 ListView 的服务内的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32123792/