BroadcastReceiver中调用Service
首先是代码:
package com.larry.msglighter; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.app.Activity; public class MyBroadcastReceiver extends BroadcastReceiver { // action 名称
String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED" ; public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( SMS_RECEIVED )) {
// 相关处理 : 地域变换、电量不足、来电来信;
Toast.makeText(context, "来短信了", Toast.LENGTH_LONG).show(); Intent serIntent = new Intent();
serIntent.setClass(context, ScreenService.class);
context.startService(serIntent); }
}
}
用Intent的setClass方法在BroadcastReceiver的onReceive()方法中调用一个Service,第一个参数写成“MyBroadcastReceiver.this”是报错的,或许是因为这个intent是在
public void onReceive(Context context, Intent intent) {}
里面调用的,所以找不到this?
解决办法是先把第一个参数改成context, just like onReceive()的第一个参数;再把startService(serIntent);改成context.startService(serIntent);。
神奇的context!到底啥意思。。先不讨论了!!
----------------------------------------
另外是一个Intent写法的问题:
这样写:
Intent serIntent = new Intent();
serIntent.setClass(context, ScreenService.class);
context.startService(serIntent);
与这样写:
Intent serIntent = new Intent(context,ScreenService.class);
context.startService(serIntent);
目测是一个效果。
先到这。
BroadcastReceiver中调用Service的更多相关文章
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- 8.1.3 在BroadcastReceiver中启动Service
2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...
- 在Java filter中调用service层方法
在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...
- springMVC在普通方法中调用service方法
SpringContextUtil类 package com.common.util; import org.springframework.beans.BeansException;import o ...
- Spring框架中,在工具类或者普通Java类中调用service或dao
spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...
- 非Controller中调用Service
1. 新增文件 package com.library.common; import org.springframework.beans.BeansException; import or ...
- SpringBoot在自定义类中调用service层等Spring其他层
解决方案: 1.上代码 @Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected He ...
- 线程中调用service方法出错
public class PnFileTGIComputeThread implements Runnable { @Resource private AppUsedService appUsedSe ...
- 关于controller中调用多个service方法的问题
一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.
随机推荐
- 嵌入式cpu架构
原文:http://www.kaixin001.com/repaste/11007221_7220618944.html### 内容: 目前主要CPU架构有ARM.X86/Atom.MIPS.Powe ...
- IntelliJ IDEA配置Tomcat/Jetty运行Web项目
一.使用Maven的POM引入插件的形式: 这种方式只需在POM中引入Tomcat/Jetty的插件即可运行.参考:http://www.cnblogs.com/EasonJim/p/6687272. ...
- Spring MVC入门实例
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"? > <web-app xmlns:xsi= ...
- python 怎么启动一个外部命令程序, 并且不阻塞当前进程
http://www.myexception.cn/perl-python/1278887.html http://blog.chinaunix.net/uid-25979788-id-3081912 ...
- springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存
1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...
- weexpack build android 和 weexpack run android 报错 及 解决方案
1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...
- VS2013 update4+Cocos2d-x 3.7 Win8下安装方法及配置
1.安装VS 2013 update4 7个G.自己就去网上找吧,一大堆,密钥问度娘. 2.安装及配置python 2.x 这里注意,一定要下载python 3.0下面的版本号. 配置:进行环境变量配 ...
- Access 执行查询时,抛出“标准表达式中数据类型不匹配”的错误
Access根据时间查询时应在查询条件前后加# SELECT COUNT(*) FROM [User] WHERE [CreateTime] > #{0}#
- apk程序查找方法调用
有android killer,现在ida对android的支持等一些方便工具,此篇(关于搜索和修改代码)废弃. 没有好的调试工具下 常用插代码(如果怕影响寄存器值,可以将.locals xxx改多几 ...
- DBExecutor android 数据库框架
https://github.com/eltld/DBExecutor android 数据库框架,sqlite database