首先是代码:

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的更多相关文章

  1. Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)

    当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...

  2. 8.1.3 在BroadcastReceiver中启动Service

    2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...

  3. 在Java filter中调用service层方法

    在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...

  4. springMVC在普通方法中调用service方法

    SpringContextUtil类 package com.common.util; import org.springframework.beans.BeansException;import o ...

  5. Spring框架中,在工具类或者普通Java类中调用service或dao

    spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...

  6. 非Controller中调用Service

    1.       新增文件 package com.library.common; import org.springframework.beans.BeansException; import or ...

  7. SpringBoot在自定义类中调用service层等Spring其他层

    解决方案: 1.上代码 @Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected He ...

  8. 线程中调用service方法出错

    public class PnFileTGIComputeThread implements Runnable { @Resource private AppUsedService appUsedSe ...

  9. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

随机推荐

  1. Centos7安装完成后一些小优化

    1.修改ip地址.网关.主机名.DNS等 [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #网 ...

  2. eclispe集成web插件

    最近公司需要使用开源框架开发,所有下载了最新版本的eclispe工具,但是在官网下载的eclispe是不包含web插件的,无法创建web项目,需要自行集成web插件 eclipse官网下载地址:htt ...

  3. Windows使用Telnet连接Linux服务器初探(待实践)

    在Windows下可以适用Telnet连接Linux服务器,但是前提是在Linux下需要安装Tlenet-Server.还要开启防火的23端口.搞定之后就可以用telnet IP进行连接. 但是,我发 ...

  4. Linux配置防火墙添加端口(Ubuntu/Debian无法使用此方法)

    注意:Ubuntu/Debian无法使用此方法 1.打开iptables vi /etc/sysconfig/iptables 2.添加防火墙规则 规则参考:http://www.cnblogs.co ...

  5. BroadcastReceiver详解(一)

    今天我们来讲一下Android中BroadcastReceiver的相关知识. BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在And ...

  6. 最大熵推导LR

    http://www.win-vector.com/dfiles/LogisticRegressionMaxEnt.pdf https://www.zhihu.com/question/2409455 ...

  7. RPM安装mysql5.6

    原文 http://blog.csdn.net/liumm0000/article/details/18841197 a. 检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名 ...

  8. php生成.php文件

    <?php // -- test.php -- // //搜集资料 $str_tmp="<?php\r\n"; //得到php的起始符.$str_tmp将累加 $str ...

  9. 一个简单演示样例来演示用PHP訪问表单变量

    首先编写表单页面orderform.html,用post方法请求服务端脚本文件:processorder.php orderform.html: <!DOCTYPE html> <h ...

  10. FSB总线、HT总线、QPI总线、DMI总线

    http://wenku.baidu.com/link?url=d0NQFyvVpU--FpQUIjkPcdE036riitKgKib3385qr7VdB1oNUcIDpRndY-HH4ITjzJln ...