Java 软件改变移动网络模式
我想为自动交换网络模式创建程序。我已经阅读了相同的主题,并且找到了部分代码,但我没有什么问题Java 软件改变移动网络模式,java,android,Java,Android,我想为自动交换网络模式创建程序。我已经阅读了相同的主题,并且找到了部分代码,但我没有什么问题 Boolean success = (Boolean) setPreferredNetwork.invoke(mTelephonyManager, networkType); 在这部分代码中,我不知道如何定义“mTelephonyManager”,有人能帮我吗?下面是几乎完整的代码 主要活动 显示 package com.example.a4genforce; i
Boolean success = (Boolean) setPreferredNetwork.invoke(mTelephonyManager,
networkType);
在这部分代码中,我不知道如何定义“mTelephonyManager”,有人能帮我吗?下面是几乎完整的代码
主要活动
显示
package com.example.a4genforce;
import * *
public class MainActivity extends Activity {
int networkType = 12; //LTE/WCDMA
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setPreferredNetwork(networkType);
}
public Method getHiddenMethod(String methodName, Class fromClass, Class[] params) {
Method method = null;
try {
Class clazz = Class.forName(fromClass.getName());
method = clazz.getMethod(methodName, params);
method.setAccessible(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return method;
}
public void setPreferredNetwork(int networkType) {
try {
Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
TelephonyManager.class, new Class[]{int.class});
Boolean success = (Boolean) setPreferredNetwork.invoke(mTelephonyManager,
networkType);
Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void onMyClick(View view) {
Toast.makeText(this, "Зачем вы нажали?", Toast.LENGTH_SHORT).show();
try {
Intent intent = new Intent("android.intent.action.MAIN");
intent.setClassName("com.android.settings", "com.android.settings.RadioInfo");
startActivity(intent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), " Device not supported", Toast.LENGTH_LONG).show();
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a4genforce">
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" tools:ignore="ProtectedPermissions"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>