我对编码非常陌生,这是我第一次使用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)@OverrIDeprotected 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);}
总结 以上是内存溢出为你收集整理的为什么我的Java代码总是在Android Studio中将信号强度打印为零全部内容,希望文章能够帮你解决为什么我的Java代码总是在Android Studio中将信号强度打印为零所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)