2016-09-21 6 views
0

まず、私はこの問題に関連するすべての回答を読んだが、私はこのエラーを解決しませんでした。私のプログラムは走っていますが、非常に遅くて遅いです。私は小さな画像を使用して、非同期タスクを使用するtryedしかし、私の問題は継続されます。ここでは、ここに私のコードがあるアプリケーションがメインスレッドで作業しすぎている可能性があります。 (アンドロイドスタジオエミュレータと本物のエミュレータ)

public class MainActivity extends AppCompatActivity { 
RecyclerView recyclerView; 
NotificationManager manager; 
public Veri veri,veri1; 
int x= 0; 

private Timer autoUpdate; 

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


    veri1 = new Veri(); 
    x=veri1.getList().size(); 

    recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view); 




    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 



    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    onResume(); 

    new AsyncCaller().execute(); 



} 

@Override 
protected void onStart() { 
    super.onStart(); 
} 
//////////////////////////////////////////// 
private class AsyncCaller extends AsyncTask<Void, Void, Void> 
{ 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 


    } 
    @Override 
    protected Void doInBackground(Void... params) { 



      autoUpdate = new Timer(); 
      autoUpdate.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        runOnUiThread(new Runnable() { 
         public void run() { 
          updateHTML(); 
         } 
        }); 
       } 
      }, 0, 60000); 


     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 


    } 

} 


//////////////////////////////////// 

private void updateHTML(){ 
    // your logic here 
    RecyclerAdapter adapter=new RecyclerAdapter(this); 
    recyclerView.setAdapter(adapter); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    veri = new Veri(); 
    int a = veri.getList().size(); 
    if(a!=x){ 
     generateNotification(MainActivity.this,"Listede Degisim Var"); 
    } 
    x=a; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Take appropriate action for each action item click 
    switch (item.getItemId()) { 

      // search action 
     case R.id.action_location_found: 
      // location found 
      LocationFound(); 
      return true; 



     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

private void LocationFound() { 
    Intent i = new Intent(MainActivity.this, LocationFound.class); 
    startActivity(i); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.activity_main_actions, menu); 

    return super.onCreateOptionsMenu(menu); 
} 
private void generateNotification(Context context,String message){ 
    int notificationId = 001; 
    Intent viewIntent = new Intent(context, MainActivity.class); 

    PendingIntent viewPendingIntent = 
      PendingIntent.getActivity(context, 0, viewIntent, 0); 

    NotificationCompat.Builder notificationBuilder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.dikat) 
        .setContentTitle("Bildirim") 
        .setContentText(message) 
        .setContentIntent(viewPendingIntent) 
        .setAutoCancel(true) 
        .setSound(Uri.parse("android.resource://" 
          + context.getPackageName() + "/" + R.raw.bildirim)) 

        .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); 

    NotificationManagerCompat notificationManager = 
      NotificationManagerCompat.from(context); 

    notificationManager.notify(notificationId, notificationBuilder.build()); 
} 

}

私のデータクラスは

public class Veri extends ActionBarActivity { 

NotificationManager manager; 
Notification myNotication; 
String ip, db, un, passwords; 
Connection connect,conn; 
PreparedStatement stmt; 
ResultSet rs; 
TextView tv; 
int position1=0; 
Button b1; 

ArrayList<String> data = new ArrayList<String>(); 
ArrayList<String> data2 = new ArrayList<String>(); 



public Veri(){ 
    ip = "x.x.x.x"; 
    un = "x"; 
    passwords = "x"; 
    db = "xxx"; 

    connect = CONN(un, passwords, db, ip); 
    String query = "SELECT Adsoyad,(DATEPART(hour, BeklemeSuresi) * 3600) + (DATEPART(minute, BeklemeSuresi) * 60) + DATEPART(second, BeklemeSuresi) as SecondsFromMidnight,Durum FROM [dbo].[IslemiDevamEdenHasta] (1)"; 



    try { 
     // connect = CONN(un, passwords, db, ip); 
     stmt = connect.prepareStatement(query); 
     rs = stmt.executeQuery(); 

     while (rs.next()) { 
      int b = (int)rs.getInt("SecondsFromMidnight")/60; 
      if(b>50) { 
       String id = rs.getString("Adsoyad"); 
       String id2 = rs.getString("Durum"); 
       String full = id + " " + id2; 
       String a = "" + b; 
       data.add(full); 
       data2.add(a); 
      } 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

} 

public ArrayList<String> getList() { 
    return data; 
} 
public ArrayList<String> getList2() { 
    return data2; 
} 

@SuppressLint("NewApi") 
private Connection CONN(String _user, String _pass, String _DB, 
         String _server) { 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    Connection conn = null; 
    String ConnURL = null; 
    try { 

     Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
     ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" 
       + "databaseName=" + _DB + ";user=" + _user + ";password=" 
       + _pass + ";"; 
     conn = DriverManager.getConnection(ConnURL); 
    } catch (SQLException se) { 
     Log.e("ERRO", se.getMessage()); 
    } catch (ClassNotFoundException e) { 
     Log.e("ERRO", e.getMessage()); 
    } catch (Exception e) { 
     Log.e("ERRO", e.getMessage()); 
    } 
    return conn; 
} 

}

、ここで私のリサイクルクラスは

​​

}

0であります

私の問題は何ですか?

+0

なぜあなたはいつもあなたのrecyclerViewを更新していますか? –

+0

ちょうどあなたのLIst を維持し、notifyDataSetChanged()メソッドを使用してください –

+0

[アプリケーションはメインスレッドであまりにも多くの作業を行っている可能性があります](http://stackoverflow.com/questions/14678593/the-application-may-be -doing-too-much-work-on-its-main-thread) – Ironman

答えて

0

あなたはそれを参照することができるようにいくつかのサードパーティのAPIがあります。したがって、画像を簡単に読み込むと便利です。また、メモリ管理コントロールもそのAPIによって処理されます。this tutorialを参照してください。

関連する問題

 関連する問題