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方法,只有查询的情况下是可以这样的.
随机推荐
- Redis数据结构之压缩列表
压缩列表是Redis为了节约内存而开发的,由一系列特殊编码的连续内存块组成的顺序型数据结构.一个压缩列表可以包含任意多个节点,每个节点可以保存一个字节数组或者一个整数值. 一.压缩列表结构1. 压缩列 ...
- LeetCode OJ--Partition List
http://oj.leetcode.com/problems/partition-list/ 链表的处理 #include <iostream> using namespace std; ...
- SSD S.M.A.R.T
经过多年HDD硬盘厂商的完善,S.M.A.R.T已经形成了一些标准,但对于SSD来说,大多数S.M.A.R.T都是自定义的,以至于每个厂商所提供的参数并不一致,但大体都会参考HDD S.M.A.R.T ...
- android 拍照预览
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- 天下文章一大抄 之 修改excel 创建时间
Sub ChangeDate()ThisWorkbook.BuiltinDocumentProperties("Creation Date") = #2 28 2016 13:25 ...
- Mac 下解决虚拟机virtualbox 4.3和windows共享问题
mac上面安装了最新的virtualbox,有些软件还是须要windows的. 1,在设置了共享之后,仍然不能使用 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...
- C#如何实现挂机锁
首先在主窗体中设置一个子窗体的实例,然后当点击挂机之后,隐藏当前窗体,同时显示子窗体. 把子窗体的背景窗体设置如下属性(主要是背景随便改成一个图片,然后FormBorderStyle改成None, ...
- SolidEdge如何复制特征 建立类似于UG 块的概念
直接Ctrl+C和Ctrl+V可以实现特征的复制粘贴 按N键可以改变特征方向 已经复制完成的特征要进行定位,则右击该特征,编辑轮廓,可以进行聪慧尺寸的标注 使用特征库的方式,就像UG的块一样, ...
- 【转载】TCP的三次握手(建立连接)和四次挥手(关闭连接)
建立连接: 理解:窗口和滑动窗口TCP的流量控制 TCP使用窗口机制进行流量控制 什么是窗口? 连接建立时,各端分配一块缓冲区用来存储接收的数据,并将缓冲区的尺寸发送给另一端 接收方发送的确认信息中包 ...
- eclipse svn org.tigris.subversion.javahl.ClientException: RA layer request failed
突然之间eclipse使用svn更新项目时报错,org.tigris.subversion.javahl.ClientException: RA layer request failed 网上搜的都是 ...