2013-02-19 22 views
7

का अभिविन्यास प्राप्त करना आपको लगता है कि एक सीधा आगे समाधान होगा। एंड्रॉइड दस्तावेज़ राज्य:एंड्रॉइड डिवाइस

ओरिएंटेशन सेंसर को एंड्रॉइड 2.2 (एपीआई लेवल 8) में बहिष्कृत किया गया था। अभिविन्यास सेंसर से कच्चे डेटा का उपयोग करने के बजाय, हम की अनुशंसा करते हैं कि आप getOrientation() विधि के साथ अभिविन्यास मानों की गणना करने के लिए getRotationMatrix() विधि का उपयोग करें।

फिर भी, वे getOrientation() और getRotationMatrix() को कार्यान्वित करने के तरीके पर समाधान प्रदान नहीं करते हैं। मैंने इन विधियों का उपयोग करके डेवलपर्स पर यहां पोस्ट के माध्यम से कई घंटे पढ़े हैं लेकिन उन सभी ने आंशिक रूप से कोड या कुछ अजीब कार्यान्वयन चिपकाया है। गुगलिंग ने एक ट्यूटोरियल प्रदान नहीं किया है। क्या कोई अभिविन्यास उत्पन्न करने के लिए इन दो विधियों का उपयोग करके एक सरल समाधान पेस्ट कर सकता है ??

+0

तो आप डिवाइस सी चाहते हैं यूरेन अभिविन्यास? –

+0

हां लेकिन इसे वास्तविक स्क्रीन से स्वतंत्र होना है। एक सेवा अभिविन्यास निर्धारित करने में सक्षम होना चाहिए। – AndroidDev

+0

आप यह निर्धारित करना चाहते हैं कि डिवाइस किसी सेवा से पोर्ट्रेट या लैंडस्केप मोड में है या नहीं? –

उत्तर

6

यहाँ getOrientation() के लिए दिया गया है:

public int getscrOrientation() 
    { 
     Display getOrient = getWindowManager().getDefaultDisplay(); 

     int orientation = getOrient.getOrientation(); 

     // Sometimes you may get undefined orientation Value is 0 
     // simple logic solves the problem compare the screen 
     // X,Y Co-ordinates and determine the Orientation in such cases 
     if(orientation==Configuration.ORIENTATION_UNDEFINED){ 

      Configuration config = getResources().getConfiguration(); 
      orientation = config.orientation; 

      if(orientation==Configuration.ORIENTATION_UNDEFINED){ 
       //if height and widht of screen are equal then 
       // it is square orientation 
       if(getOrient.getWidth()==getOrient.getHeight()){ 
        orientation = Configuration.ORIENTATION_SQUARE; 
       }else{ //if widht is less than height than it is portrait 
        if(getOrient.getWidth() < getOrient.getHeight()){ 
         orientation = Configuration.ORIENTATION_PORTRAIT; 
        }else{ // if it is not any of the above it will defineitly be landscape 
         orientation = Configuration.ORIENTATION_LANDSCAPE; 
        } 
       } 
      } 
     } 
     return orientation; // return value 1 is portrait and 2 is Landscape Mode 
    } 

और यू भी इस उदाहरण है जो दोनों तरीकों का उपयोग प्रतिनिधित्व उल्लेख कर सकते हैं:

 getOrientation and getRotationMatrix 

http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

+0

वह स्क्रीन अभिविन्यास प्राप्त करता है जिसे मैं पहले ही कोड में उपयोग करता हूं। मुझे विश्वास नहीं है कि सेंसर के आधार पर डिवाइस अभिविन्यास प्राप्त करने जैसी ही बात है। असल में, मैंने कोशिश की कि अतीत में और यह बिना किसी स्क्रीन के सेवा के अंदर काम करेगा। सेवाएं आम तौर पर स्क्रीन तक नहीं पहुंचती हैं और सेंसर से डिवाइस अभिविन्यास निर्धारित करने में सक्षम होना चाहिए। GetOrientation और getRotationMatrix आवश्यक हैं। – AndroidDev

+0

मैंने इसे आजमाया लेकिन यह नतीजे नहीं देता क्योंकि कोई उम्मीद करेगा। मैंने अभी ऐप स्टोर से एक ऐप डाउनलोड किया है जो आपके डिवाइस पर सभी सेंसर दिखाता है और वे कैसे काम करते हैं। एक को ओरिएंटेशन सेंसर कहा जाता है और जब आप इसका उपयोग करते हैं, तो ऐप दिखाता है कि सेंसर सही तरीके से कैसे काम करता है। जब आप डिवाइस को स्थानांतरित नहीं करते हैं तो मानों को नहीं बदला जाना चाहिए (या बहुत कम परिवर्तन)। उपरोक्त लिंक में कोड कोई विश्वसनीय परिणाम नहीं देता है लेकिन मूल्य सभी जगहों पर कूदते हैं। – AndroidDev

+0

मुझे लगता है कि शायद ब्रेकपॉइंट डालने और डीबग मोड में चलने से सेंसर पढ़ने के तरीके को प्रभावित कर सकते हैं। मैं डीबग किए बिना इसे चलाने और चलाने की कोशिश करूंगा और वैल्यू प्रदर्शित करता हूं क्योंकि वे अपडेट होते हैं और देखते हैं कि क्या होता है। – AndroidDev

4
public int getScreenOrientation() { 


// Query what the orientation currently really is. 
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)    { 
    return 1; // Portrait Mode 

}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    return 2; // Landscape mode 
} 
return 0; 
} 
2
protected void onResume() { 
    // change the screen orientation 
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
     setContentView(R.layout.portrait); 
    } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     setContentView(R.layout.landscape); 
    } else { 
     setContentView(R.layout.oops); 
    } 
} 
संबंधित मुद्दे