android - 在 Android 中检查互联网连接是否经常可用?

标签 android service netconnection

Hai 我的应用程序是通过 NetConnection 保存(到服务器)数据。如果网络不可用,我保存在本地,然后当网络可用时再次发送到服务器。我的问题是经常检查Internet 连接。所以我尝试了检查网络连接的服务功能。但它只调用了一次。如何解决我的问题。任何人都好心帮助我 提前致谢!

更新

package com.android.cdtech;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.view.View;

class ReceiverName extends Activity {
    BroadcastReceiver r = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        if (cm == null)
            return;
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            saveData();
        } else {
            // Do nothing or notify user somehow
        }

    }
    // code to handle broadcase

    private void saveData() {
        final saveData dh=new saveData(this); //Class for Creating a Database
        webService calService=new  webService();
        dh.open();
        Cursor c = dh.pay();

        String text = "";
        do {
            text = text + " " + "\n" + c.getString(4);
            System.out.println(c.getCount());
            // Toast.makeText(this,"Name:" +c.getString(1)+c.getString(2)+c.getString(3)+c.getString(4)+"",Toast.LENGTH_LONG).show();
            calService.paymentReceipt("PaymentReceipt", c.getString(1), c.getString(2), c.getString(3), c.getString(4), "gf", "0");
         }
         while (c.moveToNext()); 
         dh.close();
        }
    };
}

最佳答案

你可以在没有任何定时器的情况下做到这一点,只需注册接收器以监听连接变化:

<receiver android:name=".ReceiverName" >
    <intent-filter >
        <action android:name="android.net.wifi.STATE_CHANGE" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

并检查连接是否建立:

public class ReceiverName extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
        if (cm == null)
            return;
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            // Send here
        } else {
            // Do nothing or notify user somehow
        }

    }
}

关于android - 在 Android 中检查互联网连接是否经常可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633704/

相关文章:

apache-flex - flex : Random NetConnection. Call.Failed:HTTP:失败

actionscript-3 - 我的 RED5 服务器中的代码 NetConnection.Connect.SSLHandshakeFailed

javascript - Webview:阻止 JavaScript 弹出窗口

android - 查看 DDMS 中的服务

android - AdMob AdView 出现在 Android 中的 ListView 之上

c# - 停止/启动后 URI 的注册已经存在

android - aSmack 即服务

flash - AS3,NetConnection/NetStream : Is there a smart way to split source url?

带 MIC 的 Android AudioRecord 耳机

android - 号码代替铃声名称