Java 活动在运行服务时冻结
我有一个按钮,按下后,启动服务。此服务开始从设备记录信息。但是,当我按下跑步按钮时,屏幕会暂时冻结约8秒,同时记录,然后一旦完成,屏幕就会解冻,会显示一条祝酒信息(这意味着只要你按下按钮就会显示),然后我可以做任何事情。如果我再次进入应用程序屏幕,当应用程序正在记录日志时(注意:服务总是在运行,因此不会冻结它,只是记录日志),那么活动将再次冻结,并且在日志记录完成之前,不允许我做任何事 我尝试过异步任务,并在一个新的可运行/新线程上运行,但这些似乎都不适合我。我不确定是否正确地实现了它们,但我想解决这个问题 以下是服务类中的onstart命令:Java 活动在运行服务时冻结,java,android,service,android-asynctask,freeze,Java,Android,Service,Android Asynctask,Freeze,我有一个按钮,按下后,启动服务。此服务开始从设备记录信息。但是,当我按下跑步按钮时,屏幕会暂时冻结约8秒,同时记录,然后一旦完成,屏幕就会解冻,会显示一条祝酒信息(这意味着只要你按下按钮就会显示),然后我可以做任何事情。如果我再次进入应用程序屏幕,当应用程序正在记录日志时(注意:服务总是在运行,因此不会冻结它,只是记录日志),那么活动将再次冻结,并且在日志记录完成之前,不允许我做任何事 我尝试过异步任务,并在一个新的可运行/新线程上运行,但这些似乎都不适合我。我不确定是否正确地实现了它们,但我想
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// THIS WOULD BE THE METHOD THAT RUNS WHATEVER YOU WANT TO DO EVERY SO OFTEN SO FOR ME THIS IS GETTING ALL DATA ETC
getProcessInfo();
// LOG DELAY = SECONDS BETWEEN RUNNING SERVICE/METHOD. SET AT THE TOP
myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+LOG_DELAY*1000, myPendingIntent);
Intent notificationIntent = new Intent(this, LogOrCheck.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
101, notificationIntent,
PendingIntent.FLAG_NO_CREATE);
NotificationManager nm = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = this.getResources();
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.arrow_up_float)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down_float))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Androigenius")
.setContentText("Service running OK");
Notification n = builder.getNotification();
startForeground(100, n);
return Service.START_STICKY;
}
这就是我称之为startService的地方:
but1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isClicked) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Please press 'MINIMIZE' at the top to continue logging.", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 55);
toast.show();
System.out.println("Start logging");
but1.setText("Stop Logging");
but1.setTextColor(Color.RED);
// START SERVICE, DONE. (STOP IS BELOW).
startService(myIntent);
isClicked = false;
}
else if (!isClicked) {
System.out.println("Stop Logging");
but1.setText("Start Logging");
// STOP SERVICE, DONE.
stopService(myIntent);
but1.setTextColor(getResources().getColor(color.cornblue));
isClicked = true;
}
默认情况下,服务也在应用程序的主线程中运行,用户界面操作在主线程中进行。因此,如果在onStartCommand()方法中执行长任务,它将阻塞主线程
为了避免这个问题,您必须将日志记录任务迁移到一个单独的线程中。您可以使用AsycTask类来执行此操作。有关如何使用该类,请参阅。服务在导致“冻结”的UI线程上运行。使用IntentService将创建一个在后台自动运行的服务,而不是在UI线程上运行。以下是IntentService类的一些详细信息:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// THIS WOULD BE THE METHOD THAT RUNS WHATEVER YOU WANT TO DO EVERY SO OFTEN SO FOR ME THIS IS GETTING ALL DATA ETC
getProcessInfo();
// LOG DELAY = SECONDS BETWEEN RUNNING SERVICE/METHOD. SET AT THE TOP
myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+LOG_DELAY*1000, myPendingIntent);
Intent notificationIntent = new Intent(this, LogOrCheck.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
101, notificationIntent,
PendingIntent.FLAG_NO_CREATE);
NotificationManager nm = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = this.getResources();
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.arrow_up_float)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down_float))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Androigenius")
.setContentText("Service running OK");
Notification n = builder.getNotification();
startForeground(100, n);
return Service.START_STICKY;
}
我也有同样的问题。。嗯,我知道Intent服务是在一个单独的线程上运行的,但我的UI仍然冻结。我在intent服务上做了一个很长的db操作,我不明白为什么它会冻结,因为它运行在一个完全不同的线程上。