What it the alternative way to “ApnSetting.Builder” for the below android API 28 levels?

人走茶凉 提交于 2021-02-19 05:26:22

问题


I'm trying to create a mobile application to add APN(Access Point Network) into mobile application. Then I used ApnSetting.Builder() according to the android documentation follows.

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(getApplicationContext(), MainActivity.class);
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
        devicePolicyManager.addOverrideApn(componentName,apn);
    }
}

But ApnSetting.Builder() was added in the Android 9(API level 28). It not support to the below in API 28.

@RequiresApi(api = Build.VERSION_CODES.P)

But I need to create this mobile application to the below android version also, like Lollipop, Marshmallow, Nougat. So I need to know that is there an alternative way to the ApnSetting.Builder(). and then what should I do for that.


回答1:


There's nothing in ApnSetting.Builder that you can't do yourself. It's mostly methods to let you chain calls to setters to make your code easier to read. Things like:

    /**
     * Sets the proxy address of the APN.
     *
     * @param proxy the proxy address to set for the APN
     */
    @NonNull
    public Builder setProxyAddress(@Nullable String proxy) {
        this.mProxyAddress = proxy;
        return this;
    }

You can look at the source code in AOSP to see what I mean.



来源:https://stackoverflow.com/questions/59942157/what-it-the-alternative-way-to-apnsetting-builder-for-the-below-android-api-28

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!