5.24
所花时间(包括上课):2
打码量(行):210
博客量(篇):1
了解到知识点:学习实现图片拖动
package com.example.myapp;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
// 在这里执行耗时操作,例如网络请求或文件操作
String input = intent.getStringExtra("inputExtra");
Log.d(TAG, "onHandleIntent: Starting background task with input: " + input);
// 模拟一个耗时操作
try {
Thread.sleep(3000); // 休眠3秒钟,模拟耗时操作
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 任务完成后,可以通过广播或通知来通知用户
Log.d(TAG, "onHandleIntent: Background task completed");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: IntentService destroyed");
}
}
package com.example.myapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startIntentService(View view) {
Intent intent = new Intent(this, MyIntentService.class);
intent.putExtra("inputExtra", "Input data for IntentService");
startService(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/startIntentServiceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start IntentService"
android:onClick="startIntentService"
android:layout_centerInParent="true" />
</RelativeLayout>
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18209274
千万千万赵千万