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方法,只有查询的情况下是可以这样的.
随机推荐
- Yii 之视图布局
控制器代码: //设置的布局文件 public $layout = 'common'; public function actionAbout(){ $data = array('page_name' ...
- Redis数据结构之简单动态字符串
Redis没有直接使用C语言传统的字符串表示(以空字符结尾的字符数组), 而是自己构建了一种名为简单动态字符串(simple dynamic string,SDS)的抽象类型, 并将SDS用作Redi ...
- Spring基于构造函数的依赖注入(DI)
以下内容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-constructor-based-depe ...
- JAVA获取前一个月的第一天和最后一天
package com.date; import java.text.SimpleDateFormat; import java.util.Calendar; /** * 默认显示前一个月的第一天和最 ...
- windows 80端口占用情况查询
在开始-运行,输入CMD打开命令行界面,输入命令 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 具体对应 ...
- 上手ReactiveCocoa之基础篇
转自 --> http://www.jianshu.com/p/87ef6720a096 前言 很多blog都说ReactiveCocoa好用,然后各种秀自己如何灵活运用ReactiveCoco ...
- 转: NetBean远程开发的格式与过程
1. 介绍远程3种模式 https://netbeans.org/kb/docs/cnd/remote-modes.html 评注:英文的,3种模式讲的非常到位.服务器开发还是全远程比较不错.
- Android 系统广播机制
一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...
- Windows-速度优化的几个方面
One. Win+R - > cmd- > msconfig 禁用不需要的启动项! Two. 关闭一些视觉选项 Three. 设置应用启动快捷键
- 一个TAB的jquery简单写法2
<style> .honver{ color:red;} </style><script src="jquery-1.9.0.min.js">& ...