android - 即使我创建新实例(Android),AsyncTask 也无法第二次工作
问题描述
我是安卓新手。我正在学习服务。
我创建了一个前台服务 MyIntentService.java。它有一个计数器变量,每秒递增一次,并在通知中显示其值。我的服务没有任何问题。
我想在 MainActivity.java 中获取这个计数器值并在 TextView 中显示它的值。由于 Service 类中的计数器变量每秒更新(增量),因此 MainActivity 中的 TextView 应该每秒更新一次。
我知道的是我无法在主线程中更新 TextView。我必须创建一个新线程。所以,我添加了 AsyncTask。
当我第一次启动该应用程序时,该服务正在运行,并且 TextView 正在不断更新。一切都很好。但是,当我关闭应用程序(服务仍在运行)并打开它时,AsyncTask 根本不工作。TextView 没有得到更新。
这是 MainActivity.java:
public class MainActivity extends AppCompatActivity {
public boolean isServiceConnected;
long count;
private TextView tvShowSeconds;
private SwitchCompat switch11;
private Intent serviceIntent;
private ServiceConnection serviceConnection;
private MyIntentService myIntentService;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView (tvShowSeconds) displays the counter value (from the Service)
tvShowSeconds = findViewById(R.id.tvShowSeconds);
//I added switch to test if the UI is responding or not
switch11 = findViewById(R.id.switch11);
serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
bindService();
ContextCompat.startForegroundService(getApplicationContext(), serviceIntent);
new MyAsyncClass().execute("Something");
}
public void bindService() {
if (serviceConnection == null) {
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyIntentService.MyServiceBinder myServiceBinder = (MyIntentService.MyServiceBinder) service;
myIntentService = myServiceBinder.getService();
isServiceConnected = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
isServiceConnected = false;
}
};
}
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
public class MyAsyncClass extends AsyncTask<String, Long, Long> {
@Override
protected Long doInBackground(String... strings) {
try {
//Service is not binding immediately. So, I added 5 seconds delay.
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long count = 0;
while (isServiceConnected) {
//Get the counter value from the service.
count = myIntentService.getSeconds();
publishProgress(count);
}
return count;
}
@Override
protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values);
//Update the TextView with the counter value.
tvShowSeconds.setText(String.valueOf(values[0]));
}
}
}
我的要求是:
每当我启动应用程序时,应该使用计数器值不断更新 TextView。该服务应该永远运行,并且 AsyncTask 在应用程序关闭时不应该运行。它应该以 MainActivity 开始和结束。谁能告诉我该怎么做?
另外,请告诉我,在这种情况下 AsyncTask 是否正确。是否有任何以 MainActivity 开始并在 MainActivity 被销毁时停止的线程类型类(或 API)?
该应用程序仅包含两个类 MainActivity.java 和 MyIntentService.java。这是清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicesexp">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ServicesExp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyIntentService">
</service>
</application>
</manifest>
解决方案
推荐阅读
- sql - 在 Google BigQuery 标准 SQL 中将 1 添加到字符串的时间列
- amazon-web-services - Terraform s3 存储桶生命周期_规则重叠前缀
- python - 网络化 XML 文件到 Python 数据框
- flutter - Flutter 无法调用 setState 重建小部件
- xcode - xcode更新后POD无法编译
- excel - 修复了 vba 中用户定义类型的内存分配
- python - Pandas:将单元格拆分为多个列,写入 NaN
- mysql - How to get last Sunday and Sunday before last using MySQL?
- anychart - 如何在甘特图中拖放标记
- php - 在 Windows 上使用 PHP 读取文件和目录所有者