我需要检测Android设备是否已连接到Internet.
本NetworkInfo类提供了一个非静态方法isAvailable()听起来很完美.
问题是:
NetworkInfo ni = new NetworkInfo();
if (!ni.isAvailable()) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
抛出此错误:
The constructor NetworkInfo is not visible.
Run Code Online (Sandbox Code Playgroud)
安全的赌注是有另一个类返回一个NetworkInfo对象.但我不知道哪个.
我需要检测何时通过WIFI进行网络连接.发送什么广播以确定已建立有效的网络连接.我需要验证HTTP的有效网络连接是否存在.我应该注意什么以及我需要做些什么其他测试才能知道存在有效连接.
我正在开发一款能够持续保持与互联网连接的Android应用.如果Internet是dow,它应该向用户发出适当的消息.
有没有像Internet Listener这样的东西?或者如何实现此事件,每当Internet连接不可用时,它应该提供警报.
我希望从Android手机到服务器每隔15分钟到服务器发送一次位置更新.这是服务或报警管理器是最好的选择.
如果我启动服务,我可以启动asynctask将位置发布到服务器.
这是我使用的代码:
@Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
4000, 0, listener);
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("**************************************", "Location changed");
if (isBetterLocation(loc, previousBestLocation)) {
loc.getLatitude();
loc.getLongitude();
//
// intent.putExtra("Latitude", loc.getLatitude());
// intent.putExtra("Longitude", loc.getLongitude());
// intent.putExtra("Provider", loc.getProvider());
//
// sendBroadcast(intent);
}
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) { …Run Code Online (Sandbox Code Playgroud) 我正在开发一款Android应用程序,该应用程序将在工业环境中用作手持控制器.为了让平板电脑不太适合带回家,我打算一直尝试以编程方式打开飞机模式但是在4.2中已经弃用了,这也是我们用来安装Android Open Accessory的版本.但是我想如果我可以在清单中注册一个接收器,以便它可以在任何时候更改飞行模式时启动我的应用程序,然后检查它是否已关闭并提示用户重新打开它.
我尝试了以下解决方案,也找到了这个问题的答案.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
我也试过这段代码:
<receiver android:enabled="true" android:name=".ConnectivityReceiver">
<intent-filter>
<action android:name="android.intent.action.SERVICE_STATE"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
在这里也被saxos发现为aswer .
现在这些解决方案都不会导致我的应用程序在飞行模式改变时启动.
是否可以在清单文件中注册BroadcaseReceiver for Airplane Mode并让该意图启动应用程序?
编辑1:我确实在java代码中使用了BroadcastReceiver,以便在我的应用程序处于活动状态时检测到这种情况.这不是我想要的.谢谢.
编辑2:这是我目前的整个清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.i2c"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17"
android:maxSdkVersion="17"/>
<!-- SDK 14 = 4.0 ICS
SDK 17 = 4.2 JB -->
<!-- Assures software is not loaded onto unsupported screen size, i.e. phones -->
<supports-screens android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="false"
android:smallScreens="false"
android:anyDensity="true"/> …Run Code Online (Sandbox Code Playgroud)