如何获得simcard手机号码

时间:2013-02-23 11:49:21

标签: android

我想获取手机的sim卡当前号码并将其显示在Toast留言

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
  //  startActivity(browserIntent);y

    TelephonyManager phoneManager = (TelephonyManager) 
            getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String phoneNumber = phoneManager.getLine1Number();
        Context context = getApplicationContext();
        CharSequence text = phoneNumber;
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();


//   setContentView(R.layout.activity_main);       
}

但是android一直强迫它关闭。

错误日志:

02-23 13:39:29.260: E/AndroidRuntime(563): FATAL EXCEPTION: main
02-23 13:39:29.260: E/AndroidRuntime(563): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class uses-permission
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.os.Looper.loop(Looper.java:123)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 13:39:29.260: E/AndroidRuntime(563):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 13:39:29.260: E/AndroidRuntime(563):  at java.lang.reflect.Method.invoke(Method.java:521)
02-23 13:39:29.260: E/AndroidRuntime(563):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-23 13:39:29.260: E/AndroidRuntime(563):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-23 13:39:29.260: E/AndroidRuntime(563):  at dalvik.system.NativeStart.main(Native Method)
02-23 13:39:29.260: E/AndroidRuntime(563): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class uses-permission
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-23 13:39:29.260: E/AndroidRuntime(563):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.Activity.setContentView(Activity.java:1647)
02-23 13:39:29.260: E/AndroidRuntime(563):  at com.example.test.MainActivity.onCreate(MainActivity.java:23)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-23 13:39:29.260: E/AndroidRuntime(563):  ... 11 more
02-23 13:39:29.260: E/AndroidRuntime(563): Caused by: java.lang.ClassNotFoundException: android.view.uses-permission in loader dalvik.system.PathClassLoader[/data/app/com.example.test-1.apk]
02-23 13:39:29.260: E/AndroidRuntime(563):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-23 13:39:29.260: E/AndroidRuntime(563):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-23 13:39:29.260: E/AndroidRuntime(563):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
02-23 13:39:29.260: E/AndroidRuntime(563):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
02-23 13:39:29.260: E/AndroidRuntime(563):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
02-23 13:39:29.260: E/AndroidRuntime(563):  ... 20 more

1 个答案:

答案 0 :(得分:0)

你可以在androidManifest文件中设置READ_PHONE_STATE权限

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