2016-03-22 15 views
0

私は10〜15分間アクティブにする必要がある簡単なアクティビティを持っています。アクティビティではTelephonyManagerを使用して、3つのLTEパラメータ(RSRP、RSRQ、PCI)を取得しています。これらの3つのパラメータと1秒あたり1回のタイムスタンプを収集するため、UIは1秒に1回更新されます。Androidでの反応が遅く応答が遅い

LTEパラメータを取得する方法は、バックグラウンドスレッドで実行されます。毎秒更新されるUI以外に、非常に計算量が多いものはありません。しかし、私が5分以上アクティビティを実行すると、素敵なAndroid Monitorメッセージが表示されます:"I/Choreographer:91フレームをスキップしました!アプリケーションがメインスレッドであまりにも多くの作業をしている可能性があります。

私はバックグラウンドスレッドですべての努力をしていると信じていたので、何か間違っていなければなりません。アクティビティを10-15分実行すると、1,000フレームをスキップします。次に、アプリケーションを通過して別のテストを実行するためにボタンを押すと、前の15分間のテストの最後にスキップしたフレーム数がスキップされ、2回目のテストが終了すると2500までスキップできますフレーム。それから、次のアクティビティを開始するためにボタンを押すたびに(たとえ文字通り何も行われていないアクティビティであっても)ボタンを押すたびに時間がかかります。そして5つの活動だけがあります!ここで

は、私が15分間アクティブな収集データを持っている必要があります活動のスクリーンショットです:

screenshot1

と、対応するAndroidのモニターログ:ここ screenshot2

はのための私のコードですThird.javaというアクティビティ:

package com.parksjg.its.pscrindoortesttool; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.CellInfo; 
import android.telephony.CellInfoLte; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.Timer; 
import java.util.TimerTask; 

import au.com.bytecode.opencsv.CSVWriter; 

/** 
* Created by josephparks on 1/27/16. 
*/ 
public class Third extends Activity implements Runnable { 

    SignalStrengthListener signalStrengthListener; 
    TextView lteRsrp; 
    TextView lteRsrq; 
    TextView cellPciTextView, fileName; 
    ImageView img; 
    Button stopButton; 

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

    ArrayList data; 
    CSVWriter writer; 
    String mydate; 
    String startDate; 
    boolean done = false; 


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

     // This runs the code to grab the LTE parameters 
     startTele(); 

    } 

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

     // Sets up the TextViews and UI 
     setupUI(); 
     // Runs telephony method on background thread 
     run(); 
     // takes the collected data and adds it to the CSV format once per second 
     setupCSV(); 
     // Sets up the stop button, writes data to CSV, and starts next activity 
     setupButton(); 

    } 

    // This method is called by startTele() and is responsible for grabbing the LTE params 
    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); 
      } 

      // Gets the timestamp of when these LTE params where collected 
      mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 

      // Updates the UI TextViews for RSRP, RSRQ, and PCI 
      lteRsrp.setText(String.valueOf(lte1)); 
      lteRsrq.setText(String.valueOf(lte2)); 
      cellPciTextView.setText(String.valueOf(cellPci)); 

      super.onSignalStrengthsChanged(signalStrength); 

     } 
    } 

    // This takes the collected LTE data and timestamps and concatenates them into one object 
    // which is then easy to create a CSV file from 
    private void setupCSV() { 
     new Timer().scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       data.add(new String[]{mydate, lte1, lte2, String.valueOf(cellPci)}); 
      } 
     }, 0, 1000);//put here time 1000 milliseconds=1 second 

    } 

    private void setupUI() { 

     data = new ArrayList(); 

     // startDate is used to name the CSV file 
     startDate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 

     lteRsrp = (TextView) findViewById(R.id.lteRsrp); 
     lteRsrq = (TextView) findViewById(R.id.lteRsrq); 
     cellPciTextView = (TextView) findViewById(R.id.cellPciTextView); 
     fileName = (TextView) findViewById(R.id.fileName); 
     fileName.setText(startDate); 
     stopButton = (Button) findViewById(R.id.stopButton); 

     img = (ImageView) findViewById(R.id.imageView); 
     img.setImageResource(R.drawable.recording); 

//  Log.d("Time and Date", "+++++++++++++ DATE : " + mydate); 

    } 

    private 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() { 
     stopButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Toast.makeText(Third.this, "Writing output to CSV!", Toast.LENGTH_LONG).show(); 

       // Writes the data to a CSV file named by startDate 
       writeCSV(); 

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

       // Intent passes startDate and the boolean done to indicate when the CSV has been written 
       Intent intent = new Intent(getBaseContext(), Fourth.class); 
       intent.putExtra("START_DATE", startDate); 
       intent.putExtra("DONE", done); 
       startActivity(intent); 
       finish(); 

      } 
     }); 

    } 

    private void writeCSV() { 

     try { 
      File file = new File(getExternalFilesDir(null), startDate+".csv"); 
      writer = new CSVWriter(new FileWriter(file, true), ','); 
      // Headers 
      String[] headers = "Time, RSRP, RSRQ, PCI".split(","); 
      writer.writeNext(headers); 
      writer.writeAll(data); 
      writer.flush(); 
      writer.close(); 
      Toast.makeText(Third.this, "CSV Successful!", Toast.LENGTH_SHORT).show(); 
      done = true; 
//     Log.d("CSV Writer", "CSV Writer Successful!"); 

     } catch (IOException e) { 
//   Log.d("CSV Writer", "Error writing CSV file : " + e); 
      Toast.makeText(Third.this, "Error writing CSV file", Toast.LENGTH_SHORT).show(); 
     } 

    } 

} 

ここには対応者鼎のXMLはthird_activity.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="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Recording" 
     android:textSize="26sp" 
     android:textColor="#000000" 
     android:id="@+id/lteRecording" 
     android:layout_alignParentTop="true" 
     android:textAlignment="center" 
     android:background="#f91616" 
     android:textStyle="bold" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" /> 

    <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="120dp" 
     android:textAlignment="textEnd" 
     android:background="#ffdc1d" 
     android:textStyle="bold" /> 

    <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/stopButton" /> 

    <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_marginTop="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_marginTop="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" 
     android:layout_marginTop="20dp" /> 

    <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" 
     android:layout_marginTop="20dp" /> 

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

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:src="@drawable/recording" 
     android:layout_above="@+id/textView2" 
     android:layout_alignStart="@+id/lteRsrp" 
     android:layout_marginBottom="10dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Filename:" 
     android:textColor="#000000" 
     android:textSize="26sp" 
     android:id="@+id/textView6" 
     android:layout_marginTop="50dp" 
     android:layout_below="@+id/cellPciTextView" 
     android:layout_alignStart="@+id/cellPciTextView" 
     android:textStyle="bold|italic" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textColor="#000000" 
     android:textSize="26sp" 
     android:id="@+id/fileName" 
     android:layout_below="@+id/textView6" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold" /> 


</RelativeLayout> 

そして、ここではAndroidManifest.xmlを次のとおりです。

<?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"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


    <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" 
      android:noHistory="true"> 
      <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" 
      android:noHistory="true"> 
     </activity> 
     <activity android:name=".Third" 
      android:screenOrientation="portrait" 
      android:noHistory="true"> 
     </activity> 
     <activity android:name=".Fourth" 
      android:screenOrientation="portrait" 
      android:noHistory="true"> 
     </activity> 
     <activity android:name=".Final" 
      android:screenOrientation="portrait" 
      android:noHistory="true"> 
     </activity> 
    </application> 

</manifest> 

私は、バックグラウンドスレッドで実行するために、間違ったコードを入れているだろうか?これをよりスムーズかつ反応性の高いものにするにはどうすればよいですか?また、5分未満で実行すると高速ですが、これを使用して室内のLTEネットワークをテストし、歩行テスト/ドライブテストを10〜15分にする必要がありますが、停止ボタンまたは新しいテストボタンIを押すと2〜2回以内に対応する必要があります。時には、特に連続したテストの後に、ボタンを押した後でアクションが有効になるまでに20〜30秒かかります。

Flow1

Flow2

Flow3

Flow4

Flow5

:以下

は、アプリの完全な流れであります

最後のアクティビティのボタンが2番目のアクティビティを再開します。他のコードが必要な場合は教えてください。自分自身でコードを実行することに興味があれば、AndroidStudioプロジェクト全体をGitHubに投稿することもできます。

ありがとうございます!

+1

'android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND)のようなものをやって実行する必要のある時間を確認することができること以外は、'これはどのようにではありませんバックグラウンドスレッドを作成します。アクティビティのスレッドの優先度を変更する代わりに 'AsyncTask'を使うか、新しい' Thread'を作成してください。 – Titus

+0

私は新しいスレッドを作成し、そのスレッドの優先度をバックグラウンドに設定していることを理解しました。しかし、あなたは、主な活動をバックグラウンドの優先度に変更したばかりですか? – JParks

+0

はい、そうです。 – Titus

答えて

1

時間がたつにつれて構築されるものなので、参照や参照が解放されていないため、ビューやオブジェクトがガベージコレクションによって適切に収集されないということです。私は、メモリプロファイラを使用し、テスト中にどれだけのメモリが使用されているかを確認するための第一歩として提案します。あなたはあなたの方法は、この

void methodName(){ 
    long startTime = System.currentTimeMillis(); 
    . 
    . 
    . 
    . 
    Log.w("time needed for this method",Long.toString(System.currentTimeMillis()-startTime); 
} 
+0

私は約1分半でメモリが使い果たされた後、メモリの30%から40%を解放しますが、約1分でもう一度使い果たします。それは普通ですか? – JParks

+0

いいえ正常ではありません! – Anonymous

+0

またsetThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);スレッドがバックグラウンドで実行されることを意味しません。あなたはそれを行うためにAsyncTaskまたはServiceを使用します – Anonymous

関連する問題