我在获取IMEI时遇到问题没有,我已经从谷歌中删除了几乎所有代码,但仍然遇到了同样的错误,
Menifest
mod_headers
MainActivity.java
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
content_main.xml
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TelephonyManager tel;
TextView imei;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = (TextView) findViewById(R.id.textView2);
imei.setText(tel.getDeviceId().toString());
}
}
logcat的:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textColor="#4169E1"
android:textSize="18sp"
android:text="IMEI Number" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="15sp"
android:text="IMEI" />
请帮我解决我的问题
答案 0 :(得分:1)
这是因为你必须在android版本6.0中为android.permission.READ_PHONE_STATE
int permission = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_PHONE_STATE);
if(permission != PackageManager.PERMISSION_GRANTED) {
// ask permission here
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE}, 101);
} else {
// do your work here
}
上述代码应采用onCreate
活动方法。
并检查用户是否已在onRequestPermissionsResult
活动方法
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == 101) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// IMEI Getting task here
} else {
// permission denied,
// don't do the functionality that depends on this permission.
}
}
}
希望这会有所帮助。
来自this
的参考资料