我已经在下面提供了这段代码。它发布用户ID,纬度和经度,在php中更新当前用户的纬度和经度,然后执行查询,以检索位于您周围特定区域内的用户数组。 (检索年龄,名称。)因此,将填充listView,但是必须通过刷新或在打开活动时手动执行查询。但是我有一个LocationService,在其中,它有一个onLocationChanged(),它在用户位置更改时更新。每当位置更改时,我都需要执行查询/异步任务,然后在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();
}


如果您还需要其他任何内容,请说出来,我将对其进行编辑。

最佳答案

编辑:给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

使用此功能,您将知道用户的经/纬度发生任何变化!

10-08 14:58