Android 5.0以上移动网络开关

Android 5.0以前使用ConnectivityManager通过反射两个方法setMobileDataEnabled和getMobileDataEnabled来控制移动网络开和关。
Android 5.0以后使用TelephonyMananger类通过反射获取setDataEnabled和getDataEnabled类完成操作。
注意:需要使用系统权限:android:sharedUserId=”android.uid.system”。

public void setMobileDataState(Context context, boolean enabled) {
    TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled",boolean.class);
        if (null != setDataEnabled) {
            setDataEnabled.invoke(telephonyService, enabled);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public boolean getMobileDataState(Context context) {
    TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
        if (null != getDataEnabled) {
            return (Boolean) getDataEnabled.invoke(telephonyService);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
1
 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

自适应Android5.0以前及以后的代码:

TelephonyManager mTelephonyManager = (TelephonyManager)context                  .getSystemService(Context.TELEPHONY_SERVICE);
ConnectivityManager mConnectivityManager =(ConnectivityManager)context                                  .getSystemService(Context.CONNECTIVITY_SERVICE);
public void setDataEnable(){
    Object object = Build.VERSION.SDK_INT >= 21 ?       mTelephonyManager : mConnectivityManager;  
    String methodName = Build.VERSION.SDK_INT >= 21 ? "setDataEnabled" : "setMobileDataEnabled";  
    Method setMobileDataEnable;  
    try {  
        setMobileDataEnable = object.getClass().getMethod(methodName, boolean.class);  
        setMobileDataEnable.invoke(object, true);   
      } catch (Exception e) {  
            e.printStackTrace();    
      }  
}
public void checkConnectState(){
    while(true){
        Object object = Build.VERSION.SDK_INT >= 21 ? mTelephonyManager : mConnectivityManager;  
        String methodName = Build.VERSION.SDK_INT >= 21 ? "getDataEnabled" : "getMobileDataEnabled";  
         Method getMobileDataEnable;  
         boolean isDataEnabled = false;
         try {  
             getMobileDataEnable = object.getClass().getMethod(methodName, null);  
             isDataEnabled = (Boolean) getMobileDataEnable.invoke(object, null);   
            } catch (Exception e) {  
                e.printStackTrace();    
            }  
            if(isDataEnabled){
                break;
            }else{
                sleep(200);
            }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android 5.0 及以上的版本中,由于安全性增强,无法直接通过包名启动或置顶应用程序。需要您先获取应用程序的启动 `Intent`,然后使用该 `Intent` 启动或置顶应用程序。 以下是示例代码,用于根据包名启动或置顶应用程序: ```java public void openOrTopApp(Context context, String packageName) { PackageManager pm = context.getPackageManager(); Intent launchIntent = pm.getLaunchIntentForPackage(packageName); if (launchIntent != null) { // 应用程序已经安装,启动或置顶应用程序 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(Integer.MAX_VALUE); boolean isAppRunning = false; for (ActivityManager.RunningTaskInfo task : taskList) { if (task.topActivity.getPackageName().equals(packageName)) { isAppRunning = true; break; } } if (isAppRunning) { // 应用程序已经在前台运行,将其置顶 launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); } context.startActivity(launchIntent); } else { // 应用程序未安装 Toast.makeText(context, "应用程序未安装", Toast.LENGTH_SHORT).show(); } } ``` 在这个示例中,我们首先使用包管理器 `PackageManager` 获取应用程序的启动 `Intent`,如果应用程序已经安装,我们就使用该 `Intent` 启动或置顶应用程序。 如果应用程序已经在前台运行,我们就将 `Intent` 添加 `FLAG_ACTIVITY_REORDER_TO_FRONT` 标志来将其置顶;否则,我们将 `Intent` 添加 `FLAG_ACTIVITY_NEW_TASK` 标志来启动应用程序。 请注意,由于 Android 5.0 及以上版本的安全性增强,如果您想要启动或置顶其他应用程序,您需要在应用程序清单文件中声明相应的权限。例如,如果您想要启动或置顶系统应用程序,您需要在应用程序清单文件中声明 `android.permission.INTERACT_ACROSS_USERS_FULL` 权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值