質問

I'm trying to get the get the signal strength of the current cell that I'm connected to. So far, I've found this example:

http://www.android10.org/index.php/forums/44-maplocation/868-tutorialget-gsm-signal

But that is only to get a Toast when the signal is changed. Is there any way to save the value of the signal strength in a variable?

I tried this with the following code, by saving the value on a variable and getting it, but I keep getting the "initial value". Here's the code:

package org.mode.gsm;

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.widget.TextView;
import android.widget.Toast;

public class GetGsmSignalStrength extends Activity {

    TelephonyManager Tel;
    MyPhoneStateListener MyListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView abcd = new TextView(this);
        abcd.setText(getRSSI());
        setContentView(abcd);
    }

    public String getRSSI() {
        MyListener = new MyPhoneStateListener();
        Context context = (Context) getApplicationContext();
        Tel = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        String GSMCinr = "GSM CINR = " + MyListener.getCinr();
        return GSMCinr;
    }

    private class MyPhoneStateListener extends PhoneStateListener {

        String cinr = "initial value";

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            cinr = String.valueOf(signalStrength.getGsmSignalStrength());
            Toast.makeText(
                    getApplicationContext(),
                    "GSM Cinr = "
                            + String.valueOf(signalStrength
                                .getGsmSignalStrength()), Toast.LENGTH_LONG)
                    .show();
        }

        public String getCinr() {
            return cinr;
        }
    }
}

And the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.mode.gsm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".GetGsmSignalStrength" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>
    </application>

</manifest>

I would really appreciate it if anyone could help me save the value of the Signal Strength, or suggest any other way to do it...

Thanks

役に立ちましたか?

解決

GetRssi listener = new GetRssi();
protected int Rssi;
Rssi = 99;

private class GetRssi extends PhoneStateListener 
{
    @Override  
    public void onSignalStrengthsChanged(SignalStrength signalStrength)
    {
         super.onSignalStrengthsChanged(signalStrength);
         Rssi  = signalStrength.getGsmSignalStrength();
    }
    }

Rssi was declared in interger; So after you can create a function (String) which return ""+Rssi like that

public String getRssi(){
        return ""+Rssi;
}

You have to convert the value in dBm. Hope that will help!!!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top