gpt4 book ai didi

android - TelephonyManager 在 android studio 上崩溃

转载 作者:太空狗 更新时间:2023-10-29 15:36:00 27 4
gpt4 key购买 nike

我已将我的 eclipse 代码转移到 android studio,我尝试在 studio 上执行该项目,但它在 TelephonyManager 上崩溃了。我在 Theme.Holo.Light 上创建了旧代码。

我已添加权限:

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


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

类:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
if (tm.getDeviceId() != null) {
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());

String appdeviceId = deviceUuid.toString();

Toast.makeText(getBaseContext(),appdeviceId,Toast.LENGTH_LONG).show();
}
}


}

日志:

java.lang.RuntimeException: Unable to start activity ava.lang.SecurityException: Neither user 10101 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.SecurityException: Neither user 10101 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:3692)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:624)
at example.com.telephoney_manager_test.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)

最佳答案

我在 Android Studio(1.4 Beta 4)和运行 Android M 预览版的 Nexus 6 上有这个主题。

我可以通过运行一次应用程序(收到异常)来解决这个问题,然后转到设置 -> 应用程序 -> 我的应用程序 -> 权限显示(惊喜,惊喜!)所有请求的权限和所有权限都已关闭.

然后我手动打开所有权限并再次从 Android Studio 启动应用程序。从那时起,权限问题就消失了。

关于android - TelephonyManager 在 android studio 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com