Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues Passing String Data Between Two Classes
    text
    copied!<p>I need to share certain strings with my service class however when I attempt to do so I'm getting two errors stating "myWifiInfo cannot be resolved" when attempting to implement a string array using the following method (shown in the source below - and the link below):</p> <p><a href="https://stackoverflow.com/questions/5217153/passing-string-array-between-two-class-in-android-application">Passing String array between two class in android application</a></p> <p>Any suggestions? (I've been trying very hard to pass this data to a server I have running - I feel like I'm almost there - I just can't figure out why the service class can't find the strings from Main.java) </p> <p>Main.java:</p> <pre><code> Intent intent = new Intent(Main.this, Service_class.class); String[] myStrings = new String[] {"myWifiInfo.getRssi()", "myWifiInfo.getLinkSpeed()"}; intent.putExtra("strings", myStrings); startActivity(intent); </code></pre> <p>Service_class.java:</p> <pre><code> Intent intent = getIntent(); String[] myStrings = intent.getStringArrayExtra("strings"); </code></pre> <p>Main.java Full Source Code:</p> <pre><code>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("---"); } }; };} </code></pre> <p>Service Class Full Source Code:</p> <pre><code>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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload