Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android PhoneStateListener将不会发布,并且之前发布的答案均无效_Android_Telephonymanager_Phone State Listener - Fatal编程技术网

Android PhoneStateListener将不会发布,并且之前发布的答案均无效

Android PhoneStateListener将不会发布,并且之前发布的答案均无效,android,telephonymanager,phone-state-listener,Android,Telephonymanager,Phone State Listener,这个问题以前被问过多次,但没有一个答案能为我解决这个问题。我已经尝试了我能找到的每一种方法,通过按下按钮、进入onPause和onDestroy来注销我的听众;但即使在应用程序关闭时,侦听器仍然存在。更重要的是,我正在祝酒我的侦听器正在填充的数组的大小,即使当应用程序关闭时,祝酒仍然会保持并继续增加。我尝试过使用null和LISTEN_NONE以及我在网上能找到的所有东西 // Name of this file is Second.class public class Second exten

这个问题以前被问过多次,但没有一个答案能为我解决这个问题。我已经尝试了我能找到的每一种方法,通过按下按钮、进入onPause和onDestroy来注销我的听众;但即使在应用程序关闭时,侦听器仍然存在。更重要的是,我正在祝酒我的侦听器正在填充的数组的大小,即使当应用程序关闭时,祝酒仍然会保持并继续增加。我尝试过使用null和LISTEN_NONE以及我在网上能找到的所有东西

// Name of this file is Second.class
public class Second extends Activity {

    SignalStrengthListener signalStrengthListener;
    TextView lteRsrp;
    TextView lteRsrq;
    TextView cellPciTextView;
    TextView timerValue;
    Button startButton, stopButton;

    TelephonyManager tm;
    List<CellInfo> cellInfoList;
    String lte1, lte2;
    int cellPci = 0;
    long startTime = 0L;
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;
    ArrayList<String> rsrpArray;
    ArrayList<String> rsrqArray;
    ArrayList<String> pciArray;


    Handler customHandler = new Handler();
    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        flag = false;

        rsrpArray = new ArrayList<>();
        rsrqArray = new ArrayList<>();
        pciArray = new ArrayList<>();

        lteRsrp = (TextView) findViewById(R.id.lteRsrp);
        lteRsrq = (TextView) findViewById(R.id.lteRsrq);
        cellPciTextView = (TextView) findViewById(R.id.cellPciTextView);
        timerValue = (TextView) findViewById(R.id.timerValue);
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);



        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

                //start the signal strength listener
                signalStrengthListener = new SignalStrengthListener();

                ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
                tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

                try {
                    cellInfoList = tm.getAllCellInfo();
                } catch (Exception e) {
                    Log.d("SignalStrength", "+++++++++++++++++++++++++++++++++++++++++ null array spot 1: " + e);

                }


                try {
                    for (CellInfo cellInfo : cellInfoList) {
                        if (cellInfo instanceof CellInfoLte) {
                            // cast to CellInfoLte and call all the CellInfoLte methods you need
                            // Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown)
                            cellPci = ((CellInfoLte) cellInfo).getCellIdentity().getPci();
                        }
                    }
                } catch (Exception e) {
                    Log.d("SignalStrength", "++++++++++++++++++++++ null array spot 2: " + e);
                }

            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
                flag = true;

                try{
                    if(signalStrengthListener != null) {
                        tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
                        Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
                    }
                }catch(Exception e){
                    e.printStackTrace();
                    Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
                }
            }
        });


    }

    private Runnable updateTimerThread = new Runnable() {
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;
            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }
    };


    private class SignalStrengthListener extends PhoneStateListener {
        @Override
        public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {

            //++++++++++++++++++++++++++++++++++

            ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);

            tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

            String ltestr = signalStrength.toString();
            String[] parts = ltestr.split(" ");
            lte1 = parts[9];
            lte2 = parts[10];

            try {
                cellInfoList = tm.getAllCellInfo();
                for (CellInfo cellInfo : cellInfoList) {
                    if (cellInfo instanceof CellInfoLte) {
                        // cast to CellInfoLte and call all the CellInfoLte methods you need
                        // Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown)
                        cellPci = ((CellInfoLte) cellInfo).getCellIdentity().getPci();
                    }
                }
            } catch (Exception e) {
                Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e);
            }

            if (!flag) {
                rsrpArray.add(lte1);
                rsrqArray.add(lte2);
                pciArray.add(Integer.toString(cellPci));
                int size = rsrpArray.size();
                Toast.makeText(Second.this, "Array size = " + size, Toast.LENGTH_LONG).show();
            }

            lteRsrp.setText(String.valueOf(lte1));
            lteRsrq.setText(String.valueOf(lte2));
            cellPciTextView.setText(String.valueOf(cellPci));

            super.onSignalStrengthsChanged(signalStrength);

            //++++++++++++++++++++++++++++++++++++

        }
    }



    @Override
    public void onPause() {
        Log.d("onPause SigStr", "+++++++++++++++++++++++++++++++++++ onPause");
        try{
            if(signalStrengthListener != null){
                tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
                Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
            }
        }catch(Exception e){
            e.printStackTrace();
            Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
        }
        flag = true;
        super.onPause();

    }

    @Override
    public void onDestroy() {

        Log.d("onPause SigStr", "+++++++++++++++++++++++++++++++++++ onDestroy");
        try{
            if(signalStrengthListener != null) {
                tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
                Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
            }
        }catch(Exception e){
            e.printStackTrace();
            Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
        }
        flag = true;
        super.onDestroy();

    }
}
//此文件的名称为Second.class
公共课第二次扩展活动{
信号强度监听器信号强度监听器;
文本视图;
文本视图;
text视图cellPciTextView;
TextView timerValue;
按钮开始按钮、停止按钮;
TelephonyManager tm;
列表单元信息列表;
字符串lte1、lte2;
int-cellPci=0;
长启动时间=0L;
长时间单位毫秒=0L;
长时间WAPBUFF=0L;
长更新时间=0L;
ArrayList rsrpArray;
ArrayList Rrqarray;
ArrayList pciArray;
Handler customHandler=新的Handler();
布尔标志=真;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second_活动);
flag=false;
rsrpArray=newarraylist();
rsrqArray=newarraylist();
pciArray=newarraylist();
lteRsrp=(TextView)findViewById(R.id.lteRsrp);
lteRsrq=(TextView)findViewById(R.id.lteRsrq);
cellPciTextView=(TextView)findViewById(R.id.cellPciTextView);
timerValue=(TextView)findViewById(R.id.timerValue);
startButton=(按钮)findViewById(R.id.startButton);
stopButton=(按钮)findViewById(R.id.stopButton);
startButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startTime=SystemClock.uptimeMillis();
postDelayed(updateTimerThread,0);
//启动信号强度侦听器
signalStrengthListener=新的signalStrengthListener();
((电话管理器)getSystemService(电话服务)).listen(信号强度侦听器,信号强度侦听器。侦听信号强度);
tm=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
试一试{
cellInfoList=tm.getAllCellInfo();
}捕获(例外e){
Log.d(“信号强度”、“零阵列点1:”+e);
}
试一试{
用于(CellInfo CellInfo:cellInfoList){
if(CellInfoLte的cellInfo实例){
//转换到CellInfoLte并调用您需要的所有CellInfoLte方法
//获取LTE PCI:(返回物理小区Id 0..503,如果未知,则返回Integer.MAX_值)
cellPci=((CellInfoLte)cellInfo.getCellIdentity().getPci();
}
}
}捕获(例外e){
Log.d("信号强度","零位阵列点2":"e";;
}
}
});
stopButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
timeSwapBuff+=timein毫秒;
removeCallbacks(updateTimerThread);
flag=true;
试一试{
if(signalStrengthListener!=null){
tm.listen(signalStrengthListener,signalStrengthListener.listen_NONE);
Log.d(“标记”,“成功”!”);
}
}捕获(例外e){
e、 printStackTrace();
Log.d(“TAG”、“+e”+e”+;
}
}
});
}
private Runnable updateTimerThread=new Runnable(){
公开募捐{
timeinmillides=SystemClock.uptimeMillis()-startTime;
updatedTime=timeSwapBuff+Timein毫秒;
整数秒=(整数)(更新时间/1000);
整数分钟=秒/60;
秒=秒%60;
整数毫秒=(整数)(更新时间%1000);
timerValue.setText(“+mins+”:”
+字符串格式(“%02d”,秒)+”:
+格式(“%03d”,毫秒));
customHandler.postDelayed(这个,0);
}
};
私有类SignalStrengthListener扩展PhoneStateListener{
@凌驾
已更改信号强度上的公共无效(android.telephony.SignalStrength信号强度){
//++++++++++++++++++++++++++++++++++
((电话管理器)getSystemService(电话服务)).listen(信号强度侦听器,信号强度侦听器。侦听信号强度);
tm=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
字符串ltestr=signalStrength.toString();
String[]parts=ltestr.split(“”);
lte1=零件[9];
lte2=零件[10];
试一试{
cellInfoList=tm.getAllCellInfo();
用于(CellInfo CellInfo:cellInfoList){
if(CellInfoLte的cellInfo实例){
//转换到CellInfoLte并调用您需要的所有CellInfoLte方法
//获取LTE PCI:(返回物理小区Id 0..503,如果未知,则返回Integer.MAX_值)
cellPci=((CellInfoLte)cellInfo.getCellIdentity().getPci();
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#42abc0">

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"
        android:textColor="#000000"
        android:id="@+id/lteRsrp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="29dp"
        android:layout_marginTop="31dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE RSRP"
        android:textSize="16sp"
        android:textColor="#000000"
        android:id="@+id/textView2"
        android:layout_alignTop="@+id/lteRsrp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000000"
        android:textSize="16sp"
        android:id="@+id/lteRsrq"
        android:layout_below="@+id/lteRsrp"
        android:layout_alignStart="@+id/lteRsrp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE RSRQ"
        android:textSize="16sp"
        android:textColor="#000000"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"
        android:textColor="#000000"
        android:id="@+id/cellPciTextView"
        android:layout_below="@+id/lteRsrq"
        android:layout_alignStart="@+id/lteRsrq" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE PCI"
        android:textSize="16sp"
        android:textColor="#000000"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignStart="@+id/textView3" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Start"
        android:textColor="#000000"
        android:textSize="22sp"
        android:id="@+id/startButton"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@+id/textView3"
        android:layout_marginBottom="48dp"
        android:background="#ffffff"
        android:textAlignment="center"
        android:textStyle="bold"
        android:padding="4dp" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:textSize="22sp"
        android:textColor="#000000"
        android:id="@+id/stopButton"
        android:layout_alignBottom="@+id/startButton"
        android:layout_alignStart="@+id/cellPciTextView"
        android:background="#ffffff"
        android:textStyle="bold"
        android:padding="4dp"
        android:textAlignment="center" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/timerVal"
        android:textSize="40sp"
        android:textColor="#000000"
        android:id="@+id/timerValue"
        android:layout_above="@+id/startButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="55dp" />


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parksjg.its.pscrindoortesttool" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".First"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Second"
            android:screenOrientation="portrait">

        </activity>
    </application>

</manifest>