ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

多线程——异步服务IntentService

2022-07-24 19:03:15  阅读:596  来源: 互联网

标签:异步 sdf SimpleDateFormat IntentService import new android 多线程 public


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

===============================================================================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

布局:

 

<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;
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:异步,sdf,SimpleDateFormat,IntentService,import,new,android,多线程,public
来源: https://www.cnblogs.com/xiaobaibailongma/p/16515190.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有