从doInBackground

时间:2016-03-23 00:27:33

标签: java android multithreading android-asynctask

关于这个主题的问题的答案都没有解决我的具体案例的问题。

我的应用程序正在占用内存并导致主线程无响应。我正在使用监听器来监控电话状态。当状态发生变化(或每秒一次)时,我想获取相关参数并使用其值更新TextView。我尝试过使用ThreadsRunnables,但性能仍然很糟糕。现在我正在尝试AsyncTask,但我不明白。我希望这个活动至少运行15分钟,监听器在后台运行整个时间;每秒一次,我希望用侦听器抓取的值用于更新UI线程上的TextViews。

public class Second extends Activity {

    SignalStrengthListener signalStrengthListener;
    TextView lteRsrp;
    TextView lteRsrq;
    TextView cellPciTextView;

    Button startButton;

    TelephonyManager tm;
    List<CellInfo> cellInfoList;
    String lte1, lte2;
    int cellPci = 0;
//    List<String[]> data;

//    @Override
//    public void run() {
//        // Moves the current Thread into the background
//        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
//        startTele();
//
//    }

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

        setupUI();
//        run();
        new MyAsyncTask().execute();

        setupButton();

    }


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

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


                super.onSignalStrengthsChanged(signalStrength);

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

        }
    }


    private void setupUI() {
        lteRsrp = (TextView) findViewById(R.id.lteRsrp);
        lteRsrq = (TextView) findViewById(R.id.lteRsrq);
        cellPciTextView = (TextView) findViewById(R.id.cellPciTextView);
        startButton = (Button) findViewById(R.id.startButton);

    }


    public void startTele() {

        //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);

        }

    }


    private void setupButton() {
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getBaseContext(),Third.class);
                startActivity(intent);

            }
        });

    }

    //??????????????????????????????????????????????????????????????????????????????????????????????
    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

//        WeakReference<TextView> lteRsrpWRef, lteRsrqWRef, ltePciWRef;

//        public MyAsyncTask(TextView lteRsrp, TextView lteRsrq, TextView cellPciTextView) {
//            lteRsrpWRef = new WeakReference<TextView>(lteRsrp);
//            lteRsrqWRef = new WeakReference<TextView>(lteRsrq);
//            ltePciWRef = new WeakReference<TextView>(cellPciTextView);
//
//        }

        @Override
        public Void doInBackground(Void... params) {
            startTele();

            return null;
        }

        @Override
        public void onPostExecute(Void aVoid) {
            //super.onPostExecute(aVoid);

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

        }
    }
    //??????????????????????????????????????????????????????????????????????????????????????????????

}

我尝试使用AsyncTask位于&#34; //????????????&#34;的底部。我只想调用startTele(),然后从AsyncTask中的SignalStrengthListener调用doInBackground(),以便所有代码都在后台线程上运行。然后,我想获取值String lte1String lte2int cellPci,并使用它们更新UI线程上的TextView lteRsrp, lteRsrq, cellPciTextView;

这是我的Second.class XML:

<?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="#ffdc1d">

    <TextView
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="22sp"
        android:textColor="#000000"
        android:id="@+id/lteRsrp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="29dp"
        android:layout_marginTop="80dp"
        android:textAlignment="textEnd"
        android:background="#ffdc1d"
        android:textStyle="bold"
        android:layout_marginBottom="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE RSRP"
        android:textSize="22sp"
        android:textColor="#000000"
        android:id="@+id/textView2"
        android:background="#ffdc1d"
        android:textStyle="bold"
        android:layout_alignTop="@+id/lteRsrp"
        android:layout_toEndOf="@+id/startButton"
        android:layout_marginBottom="20dp" />

    <TextView
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="#a71b1b"
        android:textSize="22sp"
        android:id="@+id/lteRsrq"
        android:layout_below="@+id/lteRsrp"
        android:layout_alignStart="@+id/lteRsrp"
        android:textAlignment="textEnd"
        android:textStyle="bold"
        android:background="#ffdc1d"
        android:layout_marginBottom="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE RSRQ"
        android:textSize="22sp"
        android:textColor="#a71b1b"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:textStyle="bold"
        android:background="#ffdc1d"
        android:layout_marginBottom="20dp" />

    <TextView
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="22sp"
        android:textColor="#075f09"
        android:id="@+id/cellPciTextView"
        android:layout_below="@+id/lteRsrq"
        android:layout_alignStart="@+id/lteRsrq"
        android:textAlignment="textEnd"
        android:background="#ffdc1d"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="= LTE PCI"
        android:textSize="22sp"
        android:textColor="#075f09"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignStart="@+id/textView3"
        android:background="#ffdc1d"
        android:textStyle="bold" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Start"
        android:textColor="#ffdc1d"
        android:textSize="22sp"
        android:id="@+id/startButton"
        android:layout_marginBottom="47dp"
        android:background="#f91616"
        android:textAlignment="center"
        android:textStyle="bold"
        android:padding="4dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press the START button to begin recording"
        android:id="@+id/textView8"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#f91616"
        android:textSize="22sp"
        android:textStyle="italic"
        android:textAlignment="center"
        android:layout_marginTop="12dp" />


</RelativeLayout>

这是我的logcat消息:

enter image description here

那么,如何在后台运行我的电话监听器15分钟,同时抓取LTE参数RSRP,RSRQ和PCI,并在UI线程上使用这些值每秒更新一次TextViews?

0 个答案:

没有答案