UI线程的android异步任务错误

时间:2016-09-21 11:29:14

标签: android performance android-asynctask android-adapter

首先,我阅读了与此问题相关的所有答案,但我没有解决此错误。我的程序正在运行,但非常滞后和缓慢。我尝试使用较小的图片并使用asynctask但我的问题仍在继续。这是我的代码。我试过手机和模拟器

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 = "x";

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

这是我的回收者类

public class RecyclerAdapter extends  RecyclerView.Adapter<RecyclerViewHolder> {

public Veri veri = new Veri();
int sayi=0;

ArrayList<String> liste = veri.getList();
ArrayList<String> liste2 = veri.getList2();
static ArrayList<String> liste3 = new ArrayList<String>() ;

Context context;
LayoutInflater inflater;
public RecyclerAdapter(Context context) {
    this.context=context;
    inflater=LayoutInflater.from(context);

}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v=inflater.inflate(R.layout.item_list, parent, false);



    RecyclerViewHolder viewHolder=new RecyclerViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    liste3.add("a");
    sayi = 0;
    for (int j = 0; j < liste3.size(); j++) {
        if (liste3.get(j).equals(liste.get(position))) {
            sayi++;
        }
        if (sayi == 0) {
            holder.tv1.setText(liste.get(position));
            holder.tv2.setText(liste2.get(position));
            holder.imageView.setOnClickListener(clickListener);
            holder.imageView.setTag(holder);
        } else {
            holder.tv1.setText(liste.get(position));
            holder.tv2.setText(liste2.get(position));
            holder.imageView.setOnClickListener(clickListener);
            holder.imageView.setImageResource(R.drawable.ic_checkin);
        }
    }
}


View.OnClickListener clickListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag();
        int position = vholder.getPosition();
        //Toast.makeText(context,"This is position "+position,Toast.LENGTH_LONG ).show();
        vholder.imageView.setImageResource(R.drawable.ic_checkin);
        liste3.add(liste.get(position));
    }
};

@Override
public int getItemCount() {
    return liste.size();
}

}

我的错误实际上不是错误而是问题

09-21 11:45:08.349 20744-20744/com.enginelaldi.recyclerview I/Choreographer: Skipped 948 frames!  The application may be doing too much work on its main thread.
09-21 11:46:08.444 20744-20744/com.enginelaldi.recyclerview I/Choreographer: Skipped 954 frames!  The application may be doing too much work on its main thread.
09-21 11:47:08.348 20744-20744/com.enginelaldi.recyclerview I/Choreographer: Skipped 956 frames!  The application may be doing too much work on its main thread.
09-21 11:48:08.327 20744-20744/com.enginelaldi.recyclerview I/Choreographer: Skipped 953 frames!  The application may be doing too much work on its main thread.

0 个答案:

没有答案