android 线程太多,该应用程序可能在其主线程上做了太多工作。 (android studio模拟器和真实手机模拟器)...

此博客讨论了一个 Android 应用程序在运行时表现缓慢的问题,可能由于主线程执行过多工作导致。作者在 `MainActivity` 类中设置了定时任务更新 RecyclerView,这可能会阻塞 UI 线程。尽管尝试了使用异步任务和减小图像大小,问题仍未解决。建议优化数据加载和更新机制,避免在主线程执行耗时操作,如数据库查询和网络请求。
摘要由CSDN通过智能技术生成

首先,我阅读所有与此问题有关的答案,但我没有解决这个错误。我的程序正在运行,但非常缓慢和缓慢。我尝试使用较小的图像和使用异步任务,但我的问题仍在继续。这里是我的代码该应用程序可能在其主线程上做了太多工作。 (android studio模拟器和真实手机模拟器)

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

{

@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 data = new ArrayList();

ArrayList data2 = new ArrayList();

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 getList() {

return data;

}

public ArrayList 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;

}

}

这里是我回收类 ​​

}

我的问题是什么?

2016-09-21

Engin

+0

你为什么每次更新recyclerView? –

+0

只是维护你的LIst 和使用方法notifyDataSetChanged() –

+0

[该应用程序可能在其主线程上做了太多工作](http://stackoverflow.com/questions/14678593/the-application-maybe -anding-too-much-work-on-its-main-thread) –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值