也许这些引用和链接可以帮助您编写自己的解决方案:
1.- 获取可用网络提供商列表(完整引用如何获取可用网络提供商列表?):
由于 Android 是开源的,我查看了源代码,最后发现了一个名为INetworkQueryService的东西。我想你可以做与 android 设置实现相同的操作并与此服务进行交互。通过 NetworkSettings.java 的一些指导:
- onCreate 启动 NetworkQueryService 并绑定它。
- loadNetworksList() 告诉服务查询网络运营商。
- 评估 INetworkQueryServiceCallback,如果引发事件“EVENT_NETWORK_SCAN_COMPLETED”,将调用 networksListLoaded 以遍历可用网络。
2.- 即使快速阅读NetworkSetting.java和INetworkQueryService 接口,也能让我们了解实现您的目标。
/**
* Service connection code for the NetworkQueryService.
* Handles the work of binding to a local object so that we can make
* the appropriate service calls.
*/
/** Local service interface */
private INetworkQueryService mNetworkQueryService = null;
/** Service connection */
private final ServiceConnection mNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) log("connection created, binding local service.");
mNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as it is bound, run a query.
loadNetworksList();
}
/** Handle the task of cleaning up the local binding */
public void onServiceDisconnected(ComponentName className) {
if (DBG) log("connection disconnected, cleaning local binding.");
mNetworkQueryService = null;
}
};
- onCreate 启动 NetworkQueryService 并绑定它。
Intent intent = new Intent(this, NetworkQueryService.class);
...
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), mNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
- loadNetworksList() 告诉服务查询网络运营商。
private void loadNetworksList() {
...
// delegate query request to the service.
try {
mNetworkQueryService.startNetworkQuery(mCallback);
} catch (RemoteException e) {
}
displayEmptyNetworkList(false);
}
- INetworkQueryServiceCallback 被评估:
/**
* This implementation of INetworkQueryServiceCallback is used to receive
* callback notifications from the network query service.
*/
private final INetworkQueryServiceCallback mCallback = new INetworkQueryServiceCallback.Stub() {
/** place the message on the looper queue upon query completion. */
public void onQueryComplete(List<OperatorInfo> networkInfoArray, int status) {
if (DBG) log("notifying message loop of query completion.");
Message msg = mHandler.obtainMessage(EVENT_NETWORK_SCAN_COMPLETED,
status, 0, networkInfoArray);
msg.sendToTarget();
}
};
- 如果引发事件“EVENT_NETWORK_SCAN_COMPLETED”,则将调用 networksListLoaded 以遍历可用网络。
private void networksListLoaded(List<OperatorInfo> result, int status) {
...
if (status != NetworkQueryService.QUERY_OK) {
...
displayNetworkQueryFailed(status);
displayEmptyNetworkList(true);
} else {
if (result != null){
displayEmptyNetworkList(false);
...
} else {
displayEmptyNetworkList(true);
}
}
}
我希望它有所帮助。我认为这是一个有趣的挑战,所以下次有空的时候我会尝试一下。祝你好运!