为什么我的Java代码总是在Android Studio中将信号强度打印为零

我对编码非常陌生,这是我第一次使用TelephonyManager.我不知道为什么当我按下此代码中的按钮时,TextView只输出零.我需要它来打印信号强度.该应用程序可以编译并运行,并且具有权限,因此我现在没有想法.
谢谢,麻烦您了.

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;
    }
}

}

这是xml文件

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

从我过去所做的事情来看,andoroid:onClick函数足以满足按钮和代码中的需要.视图类被调用.

解决方法:

public void clkFrSgnl(View view){
  cap = Float.toString(sigstr);
  cdmaSignal.setText(cap);
}
上一篇:我需要ACCESS_WIFI_STATE之类的东西,但是我可以通过3g使用dataTrafiic


下一篇:CruiseControl.Net 部署到远程机器报错的解决办法