How do I find out if the GPS of an Android device is enabled

后端 未结 10 1707
暖寄归人
暖寄归人 2020-11-22 08:36

On an Android Cupcake (1.5) enabled device, how do I check and activate the GPS?

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 09:01

    Here are the steps:

    Step 1: Create services running in background.

    Step 2: You require following permission in Manifest file too:

    android.permission.ACCESS_FINE_LOCATION
    

    Step 3: Write code:

     final LocationManager manager = (LocationManager)context.getSystemService    (Context.LOCATION_SERVICE );
    
    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
      Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show(); 
    else
      Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show();
    

    Step 4: Or simply you can check using:

    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    Step 5: Run your services continuously to monitor connection.

提交回复
热议问题