Android Service BroadcastReceiver 合用 应用全局随便启动activity
来源:互联网 发布:中国网络舆情监测中心 编辑:程序博客网 时间:2024/06/14 18:56
最近公司增添新的需求,不论在应用的哪个页面都可以弹出一个界面来,今天参考着其他博客,写了一个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)
阅读全文
2 0
- Android Service BroadcastReceiver 合用 应用全局随便启动activity
- android 使用BroadcastReceiver实现开机启动Service或Activity
- Android下am命令启动Activity、Service、BroadcastReceiver
- Android下am命令启动Activity、Service、BroadcastReceiver
- BroadcastReceiver开机启动Service或Activity[转]
- 在BroadcastReceiver中启动Service或Activity
- 在BroadcastReceiver中启动Service或Activity
- Android -- BroadcastReceiver 中启动Activity
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
- 【代码】Android -- Activity 、Service、BroadcastReceiver 综合使用
- 浅谈android Activity,service,broadcastReceiver生命周期
- android 广播BroadcastReceiver(activity与service通信)
- Android Service、Activity、BroadcastReceiver简单使用
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
- Android 如何在service和BroadCastReceiver中 启动activity和alertdialog?
- Android Service启动Activity
- Android Service启动Activity .
- ul 清空 li
- J2EE系列之Spring4学习笔记(四)--IOC详解(参数注入)
- 移动互联网,博客学习
- canal完整教程(内附canal.deployer-1.0.24.tar.gz下载地址)
- DataTable的行列转换及多表头HTML表格转Excel
- Android Service BroadcastReceiver 合用 应用全局随便启动activity
- SSM 全局异常
- Leetcode34. Search for a Range
- 网络:传输层 TCP报文格式解析
- 【JavaScript】alert&document.write
- 解决Maven Update Project JDK变回1.5
- ELK(二)ElasticSearch
- 面向对象三大特征之继承
- 编程小思想整理