开发者

How to assign a method to the button?

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 onCl开发者_运维百科ick(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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜