Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

[TelephonyManager] 자기 단말의 전화번호 가져오기 본문

안드로이드/Device & Sensor

[TelephonyManager] 자기 단말의 전화번호 가져오기

설.현.아빠 2011. 2. 11. 09:48




 

 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- main.xml -->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
 android:id="@+id/mainTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

 

 

 

<?xml version="1.0" encoding="utf-8"?>

<!-- AndroidManifest.xml -->

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="lee.android.GetPhoneNumber"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GetPhoneNumberActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    
    <uses-sdk android:minSdkVersion="8" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>

 

 

// GetPhoneNumberActivity.java

 

package lee.android.GetPhoneNumber;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class GetPhoneNumberActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView textview = (TextView)findViewById(R.id.mainTextView);
        textview.setText(getMy10DigitPhoneNumber());
    }
    
    private String getMyPhoneNumber() {
     TelephonyManager mTelephonyMgr;
     mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     
     return mTelephonyMgr.getLine1Number();
    }
    private String getMy10DigitPhoneNumber() {
     String s = getMyPhoneNumber();
     return s.substring(1);
     // substrng(1)은 왼쪽 첫번째 자리를 빼고 새로운 string을 만든다는 의미
     // String s = 1234567890;
     // s.substring(7); 을 하면 s=890이 된다.
    }
}


Comments