Get Latitude and Longitude without inserting Sim Card and Start/Stop Gps from Application

后端 未结 4 621
生来不讨喜
生来不讨喜 2020-12-10 09:00

I am trying to fetch Gps latitude and longitude, but i am having a query that whenever i try to fetch Lat & Long without sim card, it is not providing any info where as

4条回答
  •  我在风中等你
    2020-12-10 09:56

    Here is my main activity with a button and I have set an onClickListner on it to toggle my GPS from OFF state to ON and there is no need of any permission to do this.
    Tested with Android 2.2 and working fine for me.

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class GPSONOFFActivity extends Activity {
    /** Called when the activity is first created. */
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
    
        b.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                toggleGPS(true);                
            }
        });
    }
    
    private void toggleGPS(boolean enable) {
        String provider = Settings.Secure.getString(getContentResolver(), 
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    
        if(provider.contains("gps") == enable) {
            return; // the GPS is already in the requested state
        }
    
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", 
            "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.sendBroadcast(poke);
     }
    }
    

提交回复
热议问题