views:

123

answers:

1

The question seems quite stupid, but I spent quite a lot of time trying to solve it successfully. The problem is that Eclipse IDE throws me a various number of different errors if I try any solutions that I used in normal Java. Another thing is that just after implementing an interface I receive an error, and Eclipse change my class to abstract. So the source code is:

public class analyzer extends Activity { TextView dateAndTimeLabel; private Button closeButton; private int signalDBm = 0;

 public class GetParams extends PhoneStateListener 
 {

     @Override  
     public void onSignalStrengthsChanged(SignalStrength signalStrength)
     {
          super.onSignalStrengthsChanged(signalStrength);
          signalDBm = signalStrength.getGsmSignalStrength();
     }
 }
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.text);
    tv.setText("Greetings, My Lord");
    EditText fld=(EditText)findViewById(R.id.field);
    fld.setText("Nothing here at the moment");  


    new GetParams();

    Button btn=(Button)findViewById(R.id.start);
    btn.setOnClickListener(this);

    btn=(Button)findViewById(R.id.stop);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }
    });

    /**updateLabel();*/
    /**Zakritie programmi*/
    this.closeButton = (Button)this.findViewById(R.id.end);
      this.closeButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          finish();
        }
      });
    /**Prosmotr loga*/

    Button btn_log=(Button)findViewById(R.id.viewlog);
    btn_log.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView tv = (TextView)findViewById(R.id.text);
            tv.setText("U pressed the button, now you will die!");
            EditText fld=(EditText)findViewById(R.id.field);
            fld.setText("Power: "  + signalDBm +" dBm\n" + "BER:... \n" + "Coordinates: ... \n");           
        };
    });
}

@Override
public void onClick(View v) {
switch (v.getId()) {
        case R.id.start:
                Toast.makeText(this, "GSM signal strength is " + signalDBm, Toast.LENGTH_SHORT).show();
        break;
        }
}

};

So there is a class GetParams with its method signalStrength.getGsmSignalStrength() that I want to use. One more thing I can add is that SignalStrength() class does not have a public constructor and this is the only way i can get to the method getGsmSignalStrength(). I would be very glad, if someone could help me because all my work stopped until I find a solution.

+1  A: 

Try the following. The PhoneStateListener simply writes the signal strength to the local variable signalDBM whenever the signal strength changes. And your activity implements the OnClickListener's method onClick(). Here you read the value of signalDBM and pass it to the toast.

package de.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class analyzer extends Activity implements OnClickListener{ 
    TextView dateAndTimeLabel;
    private Button closeButton; 
    private int signalDBM = 0;

         public class GetParams extends PhoneStateListener 
         {

             @Override  
             public void onSignalStrengthsChanged(SignalStrength signalStrength)
             {
                  super.onSignalStrengthsChanged(signalStrength);
                  signalDBM = signalStrength.getGsmSignalStrength();
             }
         }

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

             GetParams listener = new GetParams();
             TelephonyManager TelManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
             TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

             Button btn=(Button)findViewById(R.id.start);
             btn.setOnClickListener(this);             
         }


            @Override
         public void onClick(View v) {
             switch (v.getId()) {
                    case R.id.start:
                         Toast.makeText(this, "GSM signal strength is " + this.signalDBM , Toast.LENGTH_SHORT).show();
                         break;                    
            }
        }        
}

The Manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".analyzer"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
</manifest> 
Flo
I still gives me 2 errors. First one is in the line with btn.setOnClickListener(this); - it says "The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (analyzer)"Second one - it advices me to remove override before public void OnClick(View v)
StalkerRus
Still those 2 errors. I also updated the code because i noticed i did not copy it fully. I am very sorry.
StalkerRus
Ok, I updated the code one more time and added the content of the manifest.xml file. This code works on my emulator and on my device. Perhaps you must edit some values like the package name to match your requirements.
Flo
Ok, there is no more error with setonclick, but the problem with @Oveerride still exists - i continues to show error until i delete the @Override. But program seems to work. Thank you very much for your help.
StalkerRus
No Problem, if my answer helped you it would be nice if you mark it as an answer.
Flo
Sure, no problem.
StalkerRus
I have one more question. I noticed that after pressing start button multiple times the result stays constant. I bet there should be some deviations, because i don't believe in constant signal level in a radius of some 300 meters. May be something should be added to the source code?
StalkerRus
Do you test it on a real device? If you're using the emulator I think the strength won't change. On my Nexus One the code works and the strength changes depending on the phones position. You can do some logging in the onSignalStrengthsChanged() method so you can see if signal strength get updated.
Flo
I tried it on my HD2. It seems like it updates singnaldBm only once and after that the level remains constant till the next start.
StalkerRus
Have you try to provoke a signal strength loss some how to see if it really doesn't notice a change?
Flo
Yep, i did that. I don't believe the signal is so stable that it doesn't change during 5 minutes i click the button.
StalkerRus
Mh, then I don't really know what else you can do. But perhaps it's better to ask a new question for you current problem. By this way it will get more attention than here in the comments.
Flo