简体   繁体   中英

Why does my java code always print out zero for signal strength in android studio

I am pretty new to coding and this is my first time working with TelephonyManager . I can't figure out why the TextView only prints out a zero when i hit the button in this code. I need it to print out the signal strength. The app compiles and runs and the permissions are there so I am out of ideas right now. Please and thank you.

package com.example.vitaliy_2.signalminer11;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Menu extends AppCompatActivity{

TextView cdmaSignal;
Button cellButt;
String cap;

TelephonyManager tm;
MMPSL mm;
float sigstr;

int permissionCheck;


@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    cdmaSignal = (TextView) findViewById(R.id.cdmaSignal);
    cellButt = (Button) findViewById(R.id.cellButt);
    mm = new MMPSL();
    tm =  (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(mm,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

    permissionCheck = ContextCompat.checkSelfPermission(Menu.this,
            Manifest.permission.READ_PHONE_STATE);

    cap = Float.toString(sigstr);
    String nmp = "Permission required for the continued function of the phone signal monitor ";
    if(permissionCheck != PackageManager.PERMISSION_GRANTED){
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {

        } else {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    1);
        }

        cdmaSignal.setText(nmp);
    }

}
public void clkFrSgnl(View view){
    cdmaSignal.setText(cap);
  //  tts = Integer.toString(gs.signal);
    //gsmSignal.setText(tts);
}


class MMPSL extends PhoneStateListener{
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        sigstr = signalStrength.getCdmaDbm();
        sigstr = (2 * sigstr) - 113;
    }
}

}

This is the xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vitaliy_2.signalminer11.Menu">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cell_signal_CDMA"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/cdmaSignal"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="@string/get_signal_CDMA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/cellButt"
        android:onClick="clkFrSgnl" />
</RelativeLayout>

From what I've done in the past, the andoroid:onClick function was enough for the button and the in the code. the view class is called.

public void clkFrSgnl(View view){
  cap = Float.toString(sigstr);
  cdmaSignal.setText(cap);
}

I don't see an onClickListener attached to your button. The button isn't doing anything.

If the button is having it's onClickListener set elsewhere, clkFrSgnl() is not changing the text of cap so cdmaSignal.setText(cap); is just constantly resetting the text to what cap was set to in onCreate.

Try something like

onSignalStrengthsChanged(){
    super.onSignalStrengthsChanged(signalStrength);
    sigstr = signalStrength.getCdmaDbm();
    sigstr = (2 * sigstr) - 113;
    cap = sigstr;
}

I'm pretty sure the float sigstr can just be converted to the String cap , if not .toString() it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM