통신사, 전화번호 가져오기



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.gwon.blog_044;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView txtTelecom, txtPhoneNo;
 
    //manager
    TelephonyManager telephonyManager;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txtTelecom = (TextView)findViewById(R.id.txtTelecom);
        txtPhoneNo = (TextView)findViewById(R.id.txtPhoneNo);
 
        //manager
        telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
 
        //통신사 전화번호 구하기
        txtTelecom.setText("통신사 : "+getTelecomName());
        txtPhoneNo.setText("전화번호 : "+getPhoneNumber());
    }
 
    //통신사 정보
    public String getTelecomName(){
        return telephonyManager.getNetworkOperatorName();
    }
    //전화번호
    public String getPhoneNumber(){
        return telephonyManager.getLine1Number();
    }
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/txtTelecom"
        android:textSize="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <TextView
        android:id="@+id/txtPhoneNo"
        android:textSize="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
 
cs



AndroidManifest.xml)

1
2
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
 
cs



결과


+ Recent posts