AsyncTask FATAL EXCEPTION when calling a method from doInBackground

None of the answers to questions on this topic solved the problem for my specific case.

My application locks memory and prevents the main thread from being immune. I use a listener to monitor the state of telephony. When the state changes (or once per second), I want to capture the associated parameter and update the value of the TextView with its value. I tried using Threads and Runnables , but performance still sucks. Now I am trying AsyncTask , but I do not understand this. I want this activity to be performed for at least 15 minutes when the listener is working all the time in the background; and once per second, I want the values ​​captured with the listener to be used to update TextViews in the user interface stream.

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

My attempt to use AsyncTask is at the bottom, enclosed in " //???????????? ". I just want to call startTele() , which then calls SignalStrengthListener from doInBackground() in AsyncTask so that all this code runs in the background thread. Then I want to get the values ​​of String lte1 , String lte2 and int cellPci and use them to update TextView lteRsrp, lteRsrq, cellPciTextView; in the user interface thread.

Here is my XML for Second.class:

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

And here are my logcat posts:

enter image description here

So, how can I start the telephony listener in the background for 15 minutes, capturing the LTE RSRP, RSRQ and PCI parameters and updating the TextViews in the user interface thread with these values ​​once per second?

0
java android multithreading android-asynctask
Mar 23 '16 at 0:27
source share

No one has answered this question yet.

See similar questions:

97
How to run code in background thread on Android?
0
Slow and unresponsive activity in Android

or similar:

2108
How can I name one constructor from another in Java?
836
Java: when to use static methods
673
How to call a method after a delay in Android
88
AsyncTask doInBackground Android SDK not working (subclass)
2
AsyncTask does not call doInBackground methods
one
fatal exception: asynctask # 1 doInBackground
one
Calling AsyncTask from another asynthesis in doInBackground method?
0
fatal exception asynctask # 2 doInBackground
0
fatal exception asynctask # 1 doinbackground
0
Fatal Exception AsyncTask doInBackground



All Articles