安卓service的开启线程 并每隔一段时间传回主方法结果详细代码
时间: 2024-05-14 15:14:50 浏览: 23
以下是一个示例代码,其中包含一个Service类和一个MainActivity类。
Service类:
```
public class MyService extends Service {
private boolean isRunning = false;
private int counter = 0;
@Override
public void onCreate() {
super.onCreate();
isRunning = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
Log.d("MyService", "Counter: " + counter);
// 模拟耗时操作
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 发送广播通知主方法
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.example.myapp.COUNTER_UPDATE");
broadcastIntent.putExtra("counter", counter);
sendBroadcast(broadcastIntent);
counter++;
}
}
}).start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
isRunning = false;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
```
MainActivity类:
```
public class MainActivity extends AppCompatActivity {
private TextView counterTextView;
private BroadcastReceiver counterUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int counter = intent.getIntExtra("counter", 0);
counterTextView.setText("Counter: " + counter);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = findViewById(R.id.counterTextView);
// 启动Service
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
// 注册广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.myapp.COUNTER_UPDATE");
registerReceiver(counterUpdateReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止Service
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
// 注销广播接收器
unregisterReceiver(counterUpdateReceiver);
}
}
```
在上述代码中,Service类中的onStartCommand()方法中开启了一个新线程,该线程每隔5秒向Logcat输出一个计数器的值,并发送一个广播通知MainActivity类更新UI。MainActivity类中注册了一个广播接收器,用于接收Service类发送的广播,并更新UI。需要注意的是,在MainActivity类中启动Service和注册广播接收器时,需要使用相同的Action字符串,以便正确匹配广播。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)