android - 如何检查服务中的 Internet 连接以及一旦 Internet 启动服务就应该开始运行

标签 android android-service

在我的应用程序中,我正在编写一项在后台运行的服务,每小时后它会在收到新更新后立即更新设备中的本地数据库,并且我正在向用户发送状态栏通知。

在我的服务中,我正在进行休息 api 调用,但我需要检查是否互联网连接中断,用于进行 api 调用的服务不应运行。我知道如何在 Activity 中实现,但不知道如何在服务中实现。

SchedulingService.java

public class SchedulingService extends IntentService {
 DbHandler DbHandler;
 List<Shipment> _shipmentList = new ArrayList<>();

@Override
    protected void onHandleIntent(Intent intent) {
        checkNotificationStatus();
        // Waking up the Alarm Manager after some time
        TrackerAlarmReceiver.completeWakefulIntent(intent);
    }

 public void checkNotificationStatus() {
        List<String> notificationList = new ArrayList<String>();
        HashMap<User, String> notificationMap = new HashMap<User, String>();
        UserTracker userTracker;

        UserService apiService;
        DbHandler = new DbHandler(this);
        _userList = DbHandler.getAllActiveUsers();
        // Here i am making the API call
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(ApiMessage.API_URL)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new GsonConverter(new Gson()))
                .build();

        apiService = restAdapter.create(UserService.class);

     sendNotification(message);
}

最佳答案

要在 Android 应用程序中检查互联网的可用性,请使用以下方法调用 isInternetAvailable() 方法:

/*
 * Checks if WiFi or 3G is enabled or not. server
 */
public static boolean isInternetAvailable(Context context) {
    return isWiFiAvailable(context) || isMobileDateAvailable(context);
}

/**
 * Checks if the WiFi is enabled on user's device
 */
public static boolean isWiFiAvailable(Context context) {
    // ConnectivityManager is used to check available wifi network.
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network_info = connectivityManager.getActiveNetworkInfo();
    // Wifi network is available.
    return network_info != null
            && network_info.getType() == ConnectivityManager.TYPE_WIFI;
}

 /**
 * Checks if the mobile data is enabled on user's device
 */
public static boolean isMobileDateAvailable(Context context) {
    // ConnectivityManager is used to check available 3G network.
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    // 3G network is available.
    return networkInfo != null
            && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}

list .xml

    <!-- For checking the network state -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

推荐 此方法使用操作系统检查 Wifi 和移动数据的连接性。 但是 是否不可靠,因为有时用户会在没有访问互联网权限的情况下连接到其中一个,就像大多数咖啡馆和酒店发生的情况一样,直到您使用 username< 登录 和密码`

这就是为什么我建议您在服务器中创建一个简单的 HTML 文件并通过检查返回内容来PING 它。如果内容相互匹配,则说明您拥有可靠的互联网连接。

关于android - 如何检查服务中的 Internet 连接以及一旦 Internet 启动服务就应该开始运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935940/

相关文章:

android - 未调用android服务的onCreate

android - 将 Android 库移植到 BlackBerry 10

android - 如何解决 AAB 版本中的 Google Play 64 位错误?

android - 在命令行中探索 Android 嵌入式 SQLite

android - 可以从 Android 服务 onStartCommand() 调用 stopSelf() 吗?

android - android中后退键和home键的区别

android - 创建一项服务来完成 Activity 的所有艰苦工作是个好主意吗?

android - 为什么 "cocos compile -p android"给出 "The android command is no longer available."错误

java - 在android中的不同表格行中添加同一单选组的单选按钮。

android - 执行 overridePendingTransition 以从服务类启动 Activity ?