requestLocationUpdates没有响应

时间:2014-04-03 12:06:02

标签: android

我在这里尝试获取位置详细信息。为此,我同时使用了GPS_PROVIDERNETWORK_PROVIDER,两者都处于启用模式。我的问题是,LocationListener我没有收到任何回复。

public class MainActivity extends Activity{

    private TextView mTxtTodayDate;
    private Button mBtnShowTodayDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTxtTodayDate = (TextView) findViewById(R.id.txtTodayDate);
        mBtnShowTodayDate = (Button) findViewById(R.id.btnShowTodayDate);
        mBtnShowTodayDate.setOnClickListener(mShowDateListener);
    }

    private LocationListener gpsProvider= new LocationListener() {

         @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                switch (status) {
                case LocationProvider.AVAILABLE:
                    mTxtTodayDate.setText( "GPS available again\n");
                    break;
                case LocationProvider.OUT_OF_SERVICE:
                    mTxtTodayDate.setText( "GPS out of service\n");
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    mTxtTodayDate.setText("GPS temporarily unavailable\n");
                    break;
                }
            }

            @Override
            public void onProviderEnabled(String provider) {
                mTxtTodayDate.setText("GPS Provider Enabled\n");
            }

            @Override
            public void onProviderDisabled(String provider) {
                mTxtTodayDate.setText("GPS Provider Disabled\n");
            }

        @Override
        public void onLocationChanged(Location location) {
            Log.v("MainActivity", "Location Changed in location listener");
            mTxtTodayDate.setText(new SimpleDateFormat("MM/dd/yyyy", Locale
                    .getDefault()).format(new Date(location.getTime())));
        }
    };

    private LocationListener networkProvider = new LocationListener() {

         @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                switch (status) {
                case LocationProvider.AVAILABLE:
                    mTxtTodayDate.setText( "NETWORK PROVIDER available again\n");
                    break;
                case LocationProvider.OUT_OF_SERVICE:
                    mTxtTodayDate.setText( "NETWORK PROVIDER out of service\n");
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    mTxtTodayDate.setText("NETWORK PROVIDER temporarily unavailable\n");
                    break;
                }
            }

            @Override
            public void onProviderEnabled(String provider) {
                mTxtTodayDate.setText("NETWORK PROVIDER Provider Enabled\n");
            }

            @Override
            public void onProviderDisabled(String provider) {
                mTxtTodayDate.setText("NETWORK PROVIDER Provider Disabled\n");
            }

        @Override
        public void onLocationChanged(Location location) {
            Log.v("MainActivity", "Location Changed in location listener");
            mTxtTodayDate.setText(new SimpleDateFormat("MM/dd/yyyy", Locale
                    .getDefault()).format(new Date(location.getTime())));
        }
    };

    private OnClickListener mShowDateListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            getDate();
        }
    };

    private void getDate(){
        Log.v("MainActivity", "getDate()");
        LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

        boolean flag = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        boolean flag1 = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        Log.v("MainActivity", "NETWORK_PROVIDER Enabled: "+flag+" GSP:"+flag1);

        if(!flag && !flag1){
            Builder  dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Activate location listener" );
             dialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {


                 @Override
                 public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                     // TODO Auto-generated method stub
                     startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
                     paramDialogInterface.dismiss();
                 }
             });
             dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                 @Override
                 public void onClick(DialogInterface dialog, int paramInt) {
                     // TODO Auto-generated method stub
                     dialog.dismiss();
                 }
             });
             dialog.show();

        }else{
            Log.v("MainActivity", "Provider enabled");
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, networkProvider);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsProvider);
    }
}

我在清单中添加了以下权限,

<uses-permission 
        android:name="android.permission.ACCESS_FINE_LOCATION"/>

我正在做这个位置监听器只获取当天的日期,因为我的项目与日期完全相关。但应用程序用户更改日期和黑客我的应用程序功能。为避免这种情况,我计划从Location获取当天的日期。在这里,我严格不想使用INTERNET来获取当天的日期。如果对此有任何想法请与我分享。提前谢谢。

0 个答案:

没有答案