Android API 第28关如何使用addOverrideApn()方法?

How to use addOverrideApn() method in Android API level 28?

我正在尝试创建一个移动应用程序来管理 APN(接入点名称)。首先,我需要在移动应用程序中添加一个 APN。我遵循了这个文档。

https://developer.android.com/reference/android/telephony/data/ApnSetting.Builder

我做了如下操作

package com.example.myapplication;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.telephony.data.ApnSetting;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {
    private static String TAG = "MainActivity";

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an MMS proxy address with a hostname. A network might not be
        // available, so supply a dummy (0.0.0.0) IPv4 address to avoid DNS lookup.
        String host = "mms.example.com";
        byte[] ipAddress = new byte[4];
        InetAddress mmsProxy;
        try {
            mmsProxy = InetAddress.getByAddress(host, ipAddress);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return;
        }
        ApnSetting apn=null;
        try{
           apn = new ApnSetting.Builder()
                .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
                .setApnName("apn.example.com")
                .setEntryName("Example Carrier APN")
                .setMmsc(Uri.parse("http://mms.example.com:8002"))
                .setMmsProxyAddress(mmsProxy)
                .setMmsProxyPort(8799)
                .build();
        }catch (NoClassDefFoundError error){
            error.printStackTrace();
        }
        ComponentName componentName = new ComponentName(this, MainActivity.class);
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
        devicePolicyManager.addOverrideApn(componentName,apn);
    }
}

但是出现以下错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 24556
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.SecurityException: No active admin ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3160)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3303)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1991)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7258)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
    Caused by: java.lang.SecurityException: No active admin ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}
    at android.os.Parcel.createException(Parcel.java:1966)
    at android.os.Parcel.readException(Parcel.java:1934)
    at android.os.Parcel.readException(Parcel.java:1884)
    at android.app.admin.IDevicePolicyManager$Stub$Proxy.addOverrideApn(IDevicePolicyManager.java:11791)
    at android.app.admin.DevicePolicyManager.addOverrideApn(DevicePolicyManager.java:10785)
    at com.example.myapplication.MainActivity.onCreate(MainActivity.java:54)
    at android.app.Activity.performCreate(Activity.java:7353)
    at android.app.Activity.performCreate(Activity.java:7344)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3140)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3303) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1991) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:216) 
    at android.app.ActivityThread.main(ActivityThread.java:7258) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 
    Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.devicepolicy.DevicePolicyManagerService.getActiveAdminWithPolicyForUidLocked(DevicePolicyManagerService.java:3080)
    at com.android.server.devicepolicy.DevicePolicyManagerService.getActiveAdminForCallerLocked(DevicePolicyManagerService.java:3017)
    at com.android.server.devicepolicy.DevicePolicyManagerService.addOverrideApn(DevicePolicyManagerService.java:14120)
    at android.app.admin.IDevicePolicyManager$Stub.onTransact(IDevicePolicyManager.java:4339)
    at android.os.Binder.execTransact(Binder.java:739)
    Process 24556 terminated.

错误发生在下一行。

devicePolicyManager.addOverrideApn(componentName,apn);

据我所知,我需要使用 addOverrideApn() 方法来创建此移动应用程序。那我该怎么做呢?

您的应用需要成为设备所有者才能使用 addOverrideApn()。 Device Owner 首次在 Lollipop 中引入以取代 Device Admin,旨在用于 Mobile Device Management (MDM) 应用程序。您可以使用 adb 激活它以供开发/个人使用,否则必须在设置向导期间激活它,以防止通过 Play 商店安装恶意管理应用程序。