Android -- service 利用广播调用服务的方法
1. 实现原理,在Service里面注册一个广播接收者, 想要调用的时候app发送出广播,
后台的service里面的广播接收者接收到广播,并调用service里面的方法。
2. 示例代码
MainActivity.java 开启service 发出广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Intent intent = new Intent(this, MyService.class);
startService(intent);
} public void call(View v){
//发出一个特定的广播 service里面的接受器就能接收到,并调用service的方法
Intent intent = new Intent();
intent.setAction("com.kevin.callmethod");
sendBroadcast(intent);
}
MyService.java ,service 内涵广播接收者的 实现与注册
public class MyService extends Service {
private MyReceiver myReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kevin.callmethod");
registerReceiver(myReceiver, filter);
super.onCreate();
}
@Override
public void onDestroy() {
unregisterReceiver(myReceiver);
super.onDestroy();
}
private class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "接受到广播", 0).show();
methodInService();
}
}
private void methodInService(){
Toast.makeText(getApplicationContext(), "执行methodInService", 0).show();
}
}
xml 里权限注册
<service android:name="com.kevin.servicetest.MyService"/>
Android -- service 利用广播调用服务的方法的更多相关文章
- [android] 代码注册广播接收者&利用广播调用服务的方法
利用广播调用服务里面的方法,间接的方式调用服务内部的方法,与现实中差不多,请媒体曝光 主界面里面 在界面创建的时候开启一下服务普通的startService()方法 发送一条广播出去 获取Intent ...
- 代码注册广播接收者&利用广播调用服务的方法服务声命周期(混合开启)
1)说明文档: 2)效果演示: 3)代码演示:
- Android Service AIDL 远程调用服务 【简单音乐播放实例】
Android Service是分为两种: 本地服务(Local Service): 同一个apk内被调用 远程服务(Remote Service):被另一个apk调用 远程服务需要借助AIDL来完成 ...
- Android Service 通知Activity更新界面的方法研究
Android Service 通知Activity更新界面的方法研究 Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...
- 利用广播调用后台服务方法并根据方法返回的内容更新UI
一.UI布局代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- Android Service和广播
前言: 我们都知道Android的四大基本组件:Activity.ContentProvider.Service以及BroadcastReceiver,前面的两个我们在前几篇已经具体讲解了,今天这一天 ...
- 绑定方式开始服务&调用服务的方法
1.编写activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- Android应用开发-广播和服务
广播 广播的概念 现实:电台通过发送广播发布消息,买个收音机,就能收听 Android:系统在产生某个事件时发送广播,应用程序使用广播接收者接收这个广播,就知道系统产生了什么事件. Android系统 ...
- Android Service学习之本地服务
Service是在一段不定的时间运行在后台,不和用户交互应用组件.每个Service必须在manifest中 通过来声明.可以通过contect.startservice和contect.bindse ...
随机推荐
- 【BZOJ4278】[ONTAK2015]Tasowanie 后缀数组
[BZOJ4278][ONTAK2015]Tasowanie Description 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. Input 第一行包含 ...
- Testlink在CentOS、windows安装
有幸在CentOS\windows上都安装过Teslink程序,总结一下.如下: 一.CentOS安装: 1.安装包需要: xampp xampp-linux-x64-5.6.3-0-installe ...
- Android软件开发之EditText 详解(八)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xys289187120.blog.51cto.com/3361352/65718 ...
- 【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理
public static Bitmap readBitmap(String path) { BitmapFactory.Options options = new BitmapFactory.Opt ...
- 宝塔面板快速开启https服务
最近在做小程序开发,急需要一个https的域名,首先我的域名是阿里云的,服务器是腾讯云的,操作都一样: 无论阿里云还是腾讯云,配置SSL是针对服务器的,所以首先是要去申请 腾讯/阿里云服务器的SSL( ...
- Linux创建Python虚拟环境
Linux创建Python虚拟环境 安装 pip install virtualenv 基本使用 为一个工程创建一个虚拟环境: $ cd my_project $ virtualenv venv #v ...
- 170718、springboot编程之发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...
- 170530、java 迭代hashmap常用的三种方法
@SuppressWarnings("rawtypes") public class HashMapDemo { //hashMap遍历 public static void ma ...
- 报警告session_regenerate_id(): Failed to create(read) session ID: files (path: N;/path)
php.ini文件中的session.save_path = "N;/path"注释掉(前面加分号)
- Struts2表单数据接收方式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sunshoupo211/article/details/30249239 1.将Action类作 ...