我正在尝试制作一个应用程序,它可以提供 SIM 卡号、IMEI 号和电话号码。但是,当我这样做时,会出现以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hadi.sim_info/com.example.hadi.sim_info.MainActivity}: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10034 nor current process has android.permission.READ_SMS
我已经在清单中写了许可:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
这是我的应用程序代码:
public class MainActivity extends AppCompatActivity {
TextView tlf, sim, IMEI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tlf = (TextView) findViewById(R.id.Tlf_nr);
sim = (TextView) findViewById(R.id.sim);
IMEI = (TextView) findViewById(R.id.IMEI);
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
String simNR = tm.getSimSerialNumber();
String IMEINR = tm.getDeviceId();
tlf.setText("Tlf. Nr.: "+number);
sim.setText("SIM Nr.: "+simNR);
IMEI.setText("IMEI Nr.: "+ IMEINR);
}
}
编辑:
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.hadi.sim_info"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>