0

ネット接続を頻繁に検出しようとしているので、BroadcastReceiverfunctionを試してみましたが、機能していません。 5秒のものをチェックしたい

Intent Broadcast = new Intent(this,ReceiverName.class);
startActivity(Broadcast);

上記のコーディングは、クラスReceiverNameを計算するためのものです

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

    }
 };
}
4

1 に答える 1

0

次のコードを試して、ネットワーク接続を確認してください

ネットワークの可用性を確認する方法

public static boolean isNetworkPresent(Context context) {
        boolean isNetworkAvailable = false;
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        try {

            if (cm != null) {
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null) {
                    isNetworkAvailable = netInfo.isConnectedOrConnecting();
                }
            }
        } catch (Exception ex) {
            Log.e("Network Avail Error", ex.getMessage());
        }
        //check for wifi also
        if(!isNetworkAvailable){
            WifiManager connec = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            State wifi = cm.getNetworkInfo(1).getState();
            if (connec.isWifiEnabled()
                    && wifi.toString().equalsIgnoreCase("CONNECTED")) {
                isNetworkAvailable = true;
            } else {

                isNetworkAvailable = false;
            }

        }
        return isNetworkAvailable;
    }
于 2011-12-27T07:06:12.310 に答える