top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to check compass in Android

+3 votes
785 views

I have been asked this question in an interview, someone please guide me.

Which of the code is the correct way to check if a Compass sensor is present on the system? Explain your answer.

Answer A:

PackageManager m = getPackageManager();
if (!m.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {
    // This device does not have a compass, turn off the compass feature
}

Answer B:

SensorManager m = getSensorManager();
if (!m.hasSystemFeature(SensorManager.FEATURE_SENSOR_COMPASS)) {
    // This device does not have a compass, turn off the compass feature
}

Answer C:

Sensor s = getSensor();
if (!s.hasSystemFeature(Sensor.FEATURE_SENSOR_COMPASS)) {
    // This device does not have a compass, turn off the compass feature
}
posted May 23, 2016 by Abhijit

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

Answer A. is correct because PackageManager is used for retrieving various kinds of information related to the application packages that are currently installed on the device.

answer May 23, 2016 by Shivam Kumar Pandey
thanks for selecting as best ans @Abhi
Similar Questions
+2 votes

Hi, I have built an application using android programming and I want to forward & sharing image from one application to other which was loaded from a remote url.

void onShareItem(View v) {

ImageView ivImage = (ImageView) findViewById(R.id.ivResult);

Uri bmpUri = getLocalBitmapUri(ivImage);
if (bmpUri != null) {

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/*");
0 votes

Hi everyone, I have been working on android application and want to know your views on creating radar view in android with example code. I am also very curious about various other programming questions and answers because I also got stuck in some android issues, that is why I am looking for some mobile development blogs where the evolving community can help me to resolve my queries.

+1 vote

Hi, Being a learner. I am here to participate just like other android developer forum to enhance my knowledge regarding Android Programming & I would like to know that "how to debug my database and shared preferences in an android application", which has become difficult to debug. I hope any tech developer help me in resolving this issue.

...