Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在两个类之间传递字符串数据的问题_Java_Android_String_Arrays - Fatal编程技术网

Java 在两个类之间传递字符串数据的问题

Java 在两个类之间传递字符串数据的问题,java,android,string,arrays,Java,Android,String,Arrays,我需要与我的服务类共享某些字符串,但是当我尝试这样做时,在尝试使用以下方法实现字符串数组(如下面的源代码所示以及下面的链接所示)时,我收到两个错误,说明“myWifiInfo无法解析”: 有什么建议吗?(我一直在努力将这些数据传递给我正在运行的服务器——我感觉我就快到了——我就是不明白为什么服务类无法从Main.java中找到字符串) Main.java: Intent intent = new Intent(Main.this, Service_class.class);

我需要与我的服务类共享某些字符串,但是当我尝试这样做时,在尝试使用以下方法实现字符串数组(如下面的源代码所示以及下面的链接所示)时,我收到两个错误,说明“myWifiInfo无法解析”:

有什么建议吗?(我一直在努力将这些数据传递给我正在运行的服务器——我感觉我就快到了——我就是不明白为什么服务类无法从Main.java中找到字符串)

Main.java:

 Intent intent = new Intent(Main.this, Service_class.class);
        String[] myStrings = new String[] {"myWifiInfo.getRssi()", "myWifiInfo.getLinkSpeed()"};
        intent.putExtra("strings", myStrings);
        startActivity(intent); 
Service_class.java:

 Intent intent = getIntent();
        String[] myStrings = intent.getStringArrayExtra("strings");
Main.java完整源代码:

import java.util.Calendar;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

public class Main extends Activity {    

TextView textSsid, textSpeed, textRssi;

public Handler mHandler = new Handler();
public long mStartRX = 0;
public long mStartTX = 0;
public long txBytes;
public static String TAG="TEST TAG";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(Main.this, Service_class.class);
    String[] myStrings = new String[] {"myWifiInfo.getRssi()", "myWifiInfo.getLinkSpeed()"};
    intent.putExtra("strings", myStrings);
    startActivity(intent);

    PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
            0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            12 * 1000, pintent);

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), Service_class.class));

        }
    });

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), Service_class.class));
        }
    });


        textSsid = (TextView) findViewById(R.id.Ssid);
        textSpeed = (TextView) findViewById(R.id.Speed);
        textRssi = (TextView) findViewById(R.id.Rssi);
        Long.toString(mStartTX);
        Long.toString(mStartRX);
        Long.toString(txBytes);

        ParseAnalytics.trackAppOpened(getIntent());




         mStartRX = TrafficStats.getTotalRxBytes();
            mStartTX = TrafficStats.getTotalTxBytes();
            if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Uh Oh!");
                alert.setMessage("Your device does not support traffic stat monitoring.");
                alert.show();
            } else {
                mHandler.postDelayed(mRunnable, 1000);
            }}



        private final Runnable mRunnable = new Runnable() {
            public void run() {

                TextView RX = (TextView)findViewById(R.id.RX);
                TextView TX = (TextView)findViewById(R.id.TX);
                long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
                RX.setText(Long.toString(rxBytes));
                long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
                TX.setText(Long.toString(txBytes));
                mHandler.postDelayed(mRunnable, 1000);


                final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
                myChronometer.start();



                DisplayWifiState();
                this.registerReceiver(this.myWifiReceiver, new IntentFilter(
                        ConnectivityManager.CONNECTIVITY_ACTION));

            }

            private void registerReceiver(BroadcastReceiver myWifiReceiver2,
                    IntentFilter intentFilter) {
                // TODO Auto-generated method stub

            }

            private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    NetworkInfo networkInfo = (NetworkInfo) arg1
                            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        DisplayWifiState();
                    }
                }
            };

            public void DisplayWifiState() {

                ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo myNetworkInfo = myConnManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

                if (myNetworkInfo.isConnected()) {

                    textSsid.setText(myWifiInfo.getSSID());

                    textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                            + WifiInfo.LINK_SPEED_UNITS);
                    textRssi.setText(String.valueOf(myWifiInfo.getRssi()));

                } else {
                    textSsid.setText("---");

                    textSpeed.setText("---");
                    textRssi.setText("---");




                }
    };
};}
import com.parse.ParseObject; 
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

public class Service_class extends Service {
    public static String TAG="TEST TAG";
  TextView textSsid, textSpeed, textRssi;

  public Handler mHandler = new Handler();
    public long mStartRX = 0;
  public long mStartTX = 0;
  public long txBytes;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate();

        Intent intent = getIntent();
        String[] myStrings = intent.getStringArrayExtra("strings");


  textSsid = (TextView) findViewById(R.id.Ssid);
  textSpeed = (TextView) findViewById(R.id.Speed);
  textRssi = (TextView) findViewById(R.id.Rssi);
  Long.toString(mStartTX);
  Long.toString(mStartRX);
  Long.toString(txBytes);

   mStartRX = TrafficStats.getTotalRxBytes();
     mStartTX = TrafficStats.getTotalTxBytes();
     if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
          alert.setTitle("Uh Oh!");
          alert.setMessage("Your device does not support traffic stat monitoring.");
          alert.show();
     } else {
      mHandler.postDelayed(mRunnable, 1000);
     }

  }

 private Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
    }

private TextView findViewById(int speed) {
      //   TODO Auto-generated method stub
      return null;
  }

private final Runnable mRunnable = new Runnable() {
     public void run() {

         TextView RX = (TextView)findViewById(R.id.RX);
      TextView TX = (TextView)findViewById(R.id.TX);
      long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
      RX.setText(Long.toString(rxBytes));
      long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
      TX.setText(Long.toString(txBytes));
      mHandler.postDelayed(mRunnable, 1000);
      ParseObject testObject = new ParseObject("TestObject");
      testObject.put("DataOut", Long.valueOf(myWifiInfo.getRssi()));
      testObject.put("DataIn", Long.valueOf(myWifiInfo.getLinkSpeed()));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();


      final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
      myChronometer.start();



      DisplayWifiState();
      this.registerReceiver(this.myWifiReceiver, new IntentFilter(
              ConnectivityManager.CONNECTIVITY_ACTION));

  }

  private Chronometer findViewById(int chronometer) {
      //   TODO Auto-generated method stub
      return null;
  }

  private void registerReceiver(BroadcastReceiver myWifiReceiver2,
              IntentFilter intentFilter) {
         //    TODO Auto-generated method stub

      }

      private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context arg0, Intent arg1) {
         //    TODO Auto-generated method stub
          NetworkInfo networkInfo = (NetworkInfo) arg1
                  .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
          if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
              DisplayWifiState();
          }
      }
  };

  public void DisplayWifiState() {

      ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
      NetworkInfo myNetworkInfo = myConnManager
              .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

      if (myNetworkInfo.isConnected()) {

          textSsid.setText(myWifiInfo.getSSID());

          textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                  + WifiInfo.LINK_SPEED_UNITS);

          textRssi.setText(String.valueOf(myWifiInfo.getRssi()));


      } else {
          textSsid.setText("---");

          textSpeed.setText("---");
          textRssi.setText("---");
         }}};

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
     //   Log.d(TAG, "starting service");

        ParseObject testObject = new ParseObject("TestObject");

        testObject.put("DataOut", Long.valueOf(txBytes));
      testObject.put("DataIn", Long.valueOf(mStartRX));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();

        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

}
服务类完整源代码:

import java.util.Calendar;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

public class Main extends Activity {    

TextView textSsid, textSpeed, textRssi;

public Handler mHandler = new Handler();
public long mStartRX = 0;
public long mStartTX = 0;
public long txBytes;
public static String TAG="TEST TAG";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(Main.this, Service_class.class);
    String[] myStrings = new String[] {"myWifiInfo.getRssi()", "myWifiInfo.getLinkSpeed()"};
    intent.putExtra("strings", myStrings);
    startActivity(intent);

    PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
            0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            12 * 1000, pintent);

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), Service_class.class));

        }
    });

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), Service_class.class));
        }
    });


        textSsid = (TextView) findViewById(R.id.Ssid);
        textSpeed = (TextView) findViewById(R.id.Speed);
        textRssi = (TextView) findViewById(R.id.Rssi);
        Long.toString(mStartTX);
        Long.toString(mStartRX);
        Long.toString(txBytes);

        ParseAnalytics.trackAppOpened(getIntent());




         mStartRX = TrafficStats.getTotalRxBytes();
            mStartTX = TrafficStats.getTotalTxBytes();
            if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Uh Oh!");
                alert.setMessage("Your device does not support traffic stat monitoring.");
                alert.show();
            } else {
                mHandler.postDelayed(mRunnable, 1000);
            }}



        private final Runnable mRunnable = new Runnable() {
            public void run() {

                TextView RX = (TextView)findViewById(R.id.RX);
                TextView TX = (TextView)findViewById(R.id.TX);
                long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
                RX.setText(Long.toString(rxBytes));
                long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
                TX.setText(Long.toString(txBytes));
                mHandler.postDelayed(mRunnable, 1000);


                final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
                myChronometer.start();



                DisplayWifiState();
                this.registerReceiver(this.myWifiReceiver, new IntentFilter(
                        ConnectivityManager.CONNECTIVITY_ACTION));

            }

            private void registerReceiver(BroadcastReceiver myWifiReceiver2,
                    IntentFilter intentFilter) {
                // TODO Auto-generated method stub

            }

            private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    NetworkInfo networkInfo = (NetworkInfo) arg1
                            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        DisplayWifiState();
                    }
                }
            };

            public void DisplayWifiState() {

                ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo myNetworkInfo = myConnManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

                if (myNetworkInfo.isConnected()) {

                    textSsid.setText(myWifiInfo.getSSID());

                    textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                            + WifiInfo.LINK_SPEED_UNITS);
                    textRssi.setText(String.valueOf(myWifiInfo.getRssi()));

                } else {
                    textSsid.setText("---");

                    textSpeed.setText("---");
                    textRssi.setText("---");




                }
    };
};}
import com.parse.ParseObject; 
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

public class Service_class extends Service {
    public static String TAG="TEST TAG";
  TextView textSsid, textSpeed, textRssi;

  public Handler mHandler = new Handler();
    public long mStartRX = 0;
  public long mStartTX = 0;
  public long txBytes;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate();

        Intent intent = getIntent();
        String[] myStrings = intent.getStringArrayExtra("strings");


  textSsid = (TextView) findViewById(R.id.Ssid);
  textSpeed = (TextView) findViewById(R.id.Speed);
  textRssi = (TextView) findViewById(R.id.Rssi);
  Long.toString(mStartTX);
  Long.toString(mStartRX);
  Long.toString(txBytes);

   mStartRX = TrafficStats.getTotalRxBytes();
     mStartTX = TrafficStats.getTotalTxBytes();
     if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
          alert.setTitle("Uh Oh!");
          alert.setMessage("Your device does not support traffic stat monitoring.");
          alert.show();
     } else {
      mHandler.postDelayed(mRunnable, 1000);
     }

  }

 private Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
    }

private TextView findViewById(int speed) {
      //   TODO Auto-generated method stub
      return null;
  }

private final Runnable mRunnable = new Runnable() {
     public void run() {

         TextView RX = (TextView)findViewById(R.id.RX);
      TextView TX = (TextView)findViewById(R.id.TX);
      long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
      RX.setText(Long.toString(rxBytes));
      long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
      TX.setText(Long.toString(txBytes));
      mHandler.postDelayed(mRunnable, 1000);
      ParseObject testObject = new ParseObject("TestObject");
      testObject.put("DataOut", Long.valueOf(myWifiInfo.getRssi()));
      testObject.put("DataIn", Long.valueOf(myWifiInfo.getLinkSpeed()));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();


      final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
      myChronometer.start();



      DisplayWifiState();
      this.registerReceiver(this.myWifiReceiver, new IntentFilter(
              ConnectivityManager.CONNECTIVITY_ACTION));

  }

  private Chronometer findViewById(int chronometer) {
      //   TODO Auto-generated method stub
      return null;
  }

  private void registerReceiver(BroadcastReceiver myWifiReceiver2,
              IntentFilter intentFilter) {
         //    TODO Auto-generated method stub

      }

      private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context arg0, Intent arg1) {
         //    TODO Auto-generated method stub
          NetworkInfo networkInfo = (NetworkInfo) arg1
                  .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
          if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
              DisplayWifiState();
          }
      }
  };

  public void DisplayWifiState() {

      ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
      NetworkInfo myNetworkInfo = myConnManager
              .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

      if (myNetworkInfo.isConnected()) {

          textSsid.setText(myWifiInfo.getSSID());

          textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                  + WifiInfo.LINK_SPEED_UNITS);

          textRssi.setText(String.valueOf(myWifiInfo.getRssi()));


      } else {
          textSsid.setText("---");

          textSpeed.setText("---");
          textRssi.setText("---");
         }}};

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
     //   Log.d(TAG, "starting service");

        ParseObject testObject = new ParseObject("TestObject");

        testObject.put("DataOut", Long.valueOf(txBytes));
      testObject.put("DataIn", Long.valueOf(mStartRX));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();

        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

}

myWifiInfo
未在您使用它的位置声明/可见。用
WifiInfo myWifiInfo=myWifiManager.getConnectionInfo()实例化它与上面在run()方法中所做的相同。
仅仅因为您已经在代码中的某个地方声明了
myWifiInfo
,并不意味着您可以在任何地方使用它。
myWifiInfo
的声明在run方法中不可见。它被称为“范围”

因此,改变这一点:

  testObject.put("DataOut", Long.valueOf(myWifiInfo.getRssi()));
  testObject.put("DataIn", Long.valueOf(myWifiInfo.getLinkSpeed()));


当您编写
newrunnable(){…}
时,您正在实例化一个内联类;insede this class
引用类本身。您指的是
this.myWifiReceiver
,但我看不到
myWifiReceiver
对象的声明位置

btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
ntent intent = new Intent(Main.this, Service_class.class);

   intent.putExtra("strings", myStrings);
    startActivity(intent);
            startService(new Intent(getBaseContext(), Service_class.class));


        }
    });

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
ntent intent = new Intent(Main.this, Service_class.class);
           intent.putExtra("strings", myStrings);
    startActivity(intent);
            stopService(new Intent(getBaseContext(), Service_class.class));
        }
    });

尝试此操作…myStrings全局声明和delcare意图并将值行写入按钮单击事件。

您的错误是eclipse警告还是应用程序崩溃?错误在哪一行?myWifiInfo无法解析服务\u class.java/ParseStarterProject/src/com/parse/starter行80 myWifiInfo无法解析已解析的Service_class.java/ParseStarterProject/src/com/parse/starter行81。检查“如何在单个应用程序中的活动/服务之间传递数据”下的主题?使用共享偏好。太棒了!加上那条线导致了两个我即将消失的错误。。。(然而,在实现它之后,我有一个新的错误:myWifiManager无法解决)我非常确定这个错误有相同的原因,请参阅Alepac的回答。我是否应该以与Main.java中相同的方式声明它?private BroadcastReceiver myWifiReceiver=新的BroadcastReceiver();当我尝试这样做时,我也会得到一个错误(无法实例化广播接收器的类型)-你们都回答了我的问题!我应该如何决定你们中的哪一位提供了正确的答案?顺便说一句,当我实现这一点时,我得到一个错误,声明“ActivityNotFoundException:找不到显式活动类”,这很奇怪,因为我的清单中有以下内容