在Android中检查Internet连接是否经常可用?

时间:2011-12-26 07:11:00

标签: android service netconnection

Hai我的应用程序是通过NetConnection保存(到服务器)数据。如果net不可用,我在本地保存,然后当net可用时再发送到服务器。我的问题是检查免费上网连接。所以我尝试了服务功能来检查网络连接。但它只召唤一次。如何解决我的问题。有人帮助我 提前谢谢!

更新

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();
        }
    };
}

2 个答案:

答案 0 :(得分:13)

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

<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
        }

    }
}

答案 1 :(得分:0)

你应该在TimerTask而不是Service中编写代码,因为OnCreate()只会在整个Service Life周期内执行一次。

mTimerTask = new TimerTask() {

    @Override
    public void run() {

        ConnectivityManager conMgr = (ConnectivityManager)   getSystemService      (Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null &&  conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
             Toast.makeText(this, "net Started", Toast.LENGTH_LONG).show();
             saveData();
        }

     }
};

//每隔5秒检查一次互联网连接

mTimer = new Timer();
mTimer.scheduleAtFixedRate(mTimerTask,1000,5000);