Broadcast组件——收发广播应用——监听系统广播——接收分钟到达广播

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

布局:

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_minute"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="17sp" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

 

 

 

 

 

 

 

 

 

 

 

代码:

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{

    private TextView tv_minute; // 声明一个文本视图对象

    private String desc = "开始侦听分钟广播,请稍等。注意要保持屏幕亮着,才能正常收到广播";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv_minute = findViewById(R.id.tv_minute);
        tv_minute.setText(desc);
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        timeReceiver = new TimeReceiver(); // 创建一个分钟变更的广播接收器

        // 创建一个意图过滤器,只处理系统分钟变化的广播
        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);

        registerReceiver(timeReceiver, filter); // 注册接收器,注册之后才能正常接收广播
    }

    @Override
    protected void onStop()
    {
        super.onStop();

        unregisterReceiver(timeReceiver); // 注销接收器,注销之后就不再接收广播
    }

    private TimeReceiver timeReceiver; // 声明一个分钟广播的接收器实例

    // 定义一个分钟广播的接收器
    private class TimeReceiver extends BroadcastReceiver
    {
        // 一旦接收到分钟变更的广播,马上触发接收器的onReceive方法
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent != null)
            {
                desc = String.format("%s\n%s 收到一个分钟到达广播%s", desc, DateUtil.getNowTime(), intent.getAction());

                tv_minute.setText(desc);
            }
        }
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

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

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-07-23 14:37  小白龙白龙马  阅读(47)  评论(0编辑  收藏  举报