



===============================================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_intent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="点我看看有没有反应"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_intent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看点击结果"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>


代码:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private TextView tv_intent;
private Handler mHandler = new Handler(Looper.myLooper()); // 声明一个处理器对象
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_intent = findViewById(R.id.tv_intent);
findViewById(R.id.btn_intent).setOnClickListener(this);
mHandler.postDelayed(mService, 100); // 延迟100毫秒后启动异步任务
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_intent)
{
tv_intent.setText(DateUtil.getNowTime() + " 您轻轻点了一下下(异步服务正在运行,不影响您在界面操作)");
}
}
private Runnable mService = new Runnable()
{
@Override
public void run()
{
// 构建通往异步服务的意图
Intent intent = new Intent(MainActivity.this, AsyncService.class);
startService(intent); // 启动意图设定的异步服务
}
};
}


AsyncService
package com.example.myapplication;
import android.content.Intent;
import android.app.IntentService;
import android.util.Log;
public class AsyncService extends IntentService
{
private static final String TAG = "AsyncService";
public AsyncService()
{
super("com.example.chapter11.service.AsyncService");
}
// onStartCommand运行于主线程
public int onStartCommand(Intent intent, int flags, int startid)
{
Log.i(TAG, "onStartCommand");
// 试试在onStartCommand里面沉睡,页面按钮是不是无法点击了?
// try {
// Thread.sleep(30*1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
return super.onStartCommand(intent, flags, startid);
}
// onHandleIntent运行于分线程
protected void onHandleIntent(Intent intent)
{
Log.d(TAG, "begin onHandleIntent");
// 在onHandleIntent这里执行耗时任务,不会影响页面的处理
try
{
Thread.sleep(30 * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Log.d(TAG, "end onHandleIntent");
}
}


DateUtil
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@SuppressLint("SimpleDateFormat")
public class DateUtil
{
// 获取当前的日期时间
public static String getNowDateTime(String formatStr)
{
String format = formatStr;
if (TextUtils.isEmpty(format))
{
format = "yyyyMMddHHmmss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
// 获取当前的时间
public static String getNowTime()
{
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
// 获取当前的时间(精确到毫秒)
public static String getNowTimeDetail()
{
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
return sdf.format(new Date());
}
public static String getNowDate()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(new Date());
}
public static String getDate(Calendar calendar)
{
Date date = calendar.getTime();
// 创建一个日期格式化的工具
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 将当前日期时间按照指定格式输出格式化后的日期时间字符串
return sdf.format(date);
}
public static String getMonth(Calendar calendar)
{
Date date = calendar.getTime();
// 创建一个日期格式化的工具
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
// 将当前日期时间按照指定格式输出格式化后的日期时间字符串
return sdf.format(date);
}
public static Date formatString(String strTime)
{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
date = sdf.parse(strTime);
}
catch (Exception e)
{
e.printStackTrace();
}
return date;
}
}










原创文章,作者:jamestackk,如若转载,请注明出处:https://blog.ytso.com/tech/276718.html