Android Service BroadcastReceiver 合用 应用全局随便启动activity

最近公司增添新的需求,不论在应用的哪个页面都可以弹出一个界面来,今天参考着其他博客,写了一个demo并记录在此。

首先,创建一个首页的页面。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jingsong.myservicetest.MainActivity">

    <EditText
        android:id="@+id/ed_hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请填写24小时制时间"
        android:inputType="number" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorPrimary" />

    <EditText
        android:id="@+id/ed_minute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="填写分钟"
        android:inputType="number" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorPrimary" />

    <Button
        android:id="@+id/btnStartService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="开启服务" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorPrimary" />

    <Button
        android:id="@+id/btn_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="开启第一个页面" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorPrimary" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="这是main页面 ---  首页 - -- -" />


</LinearLayout>

这个布局很简单,就不多说了。在首页里设置点击事件。

switch (view.getId()) {
            case R.id.btnStartService:

                intent = new Intent(MainActivity.this, MyService.class);
                ed_hours.setVisibility(View.INVISIBLE);
                ed_minute.setVisibility(View.INVISIBLE);
                btnStartService.setVisibility(View.INVISIBLE);

                intent.putExtra("Hour", tag_hours + "," + tag_minute);// 传送时间
                startService(intent);

                imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
                break;
            case R.id.btn_01:
                intent = new Intent(this, OneActivity.class);
                startActivity(intent);

                break;

        }

并创建服务,和启动的其他的界面。那么下面是服务里的代码是这样的。

@Override
    public void onCreate() {
        Log.e(TAG, "ExampleService-onCreate");

     hour = intent.getStringExtra("Hour");// 这是activity传过了的时间值。
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(receiver, filter);

        super.onCreate();
    }

在onCreat 里面注册一个广播,并在服务里new 一个广播作为类变量,用来接收和处理数据。

private final BroadcastReceiver receiver = new BroadcastReceiver() {

        private String tamp_hour;
        private String tamp_minuts;

        @Override
        public void onReceive(final Context context, Intent intent) {

            if (!(TextUtils.isEmpty(hour))) {
                String[] split = hour.split(",");

                tamp_hour = split[0];
                tamp_minuts = split[1];

            }

            String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_TICK)) {

                //do what you want to do ...13

                String time1 = new SimpleDateFormat("HH:mm:ss").format(new Date());//获取系统时间

                String[] split = time1.split(":");

                mHour = Integer.parseInt(split[0]);
                mMinuts = Integer.parseInt(split[1]);


                if (mHour == Integer.parseInt(tamp_hour) && (mMinuts == Integer.parseInt(tamp_minuts))) {

                    intent = new Intent(context, FileBrowserActivity.class);

                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(intent);


                }

            }
        }
    };

这里的代码只有它 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);比较厉害了。
可以直接将你想要启动 的 activity直接推到应用栈的最前面。
如果你启动了这应用的服务以后, 将这个应用退到后台运行,或你有启动了其他应用时,只要时间已到,intent会调用这个方法setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),直接将这个应用推到其他应用栈的最前面,这个 activity也会直接推到这应用栈的最前面。

也许我写的demo比较简单,大神表喷我啊。

[这是demo地址(https://github.com/zhangsong1990/MyServiceTest)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值