首先是代码:

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. 隐藏video标签的下载按钮

    问题: 使用video标签时,有些浏览器会显示视频的下载按钮,而这并不是我们需要的功能,必须想办法去掉. 解决方法: 使用下面的css可以达到隐藏下载按钮的效果,但是点击下载的位置,还是能出现开始下载 ...

  2. android:logo

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  3. Spark学习(一) Spark初识

    一.官网介绍 1.什么是Spark 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于A ...

  4. JQuery select 编程时选中原有的值

    js 此为核心代码 $(document).ready(function(){ $("#carTypeId").attr("value",'${carInfo. ...

  5. 【mac】屏幕截图快捷键

    这里只说四种 1.command+shift+4 截图是区域截图,会自动保存在桌面上 2.command+shift+control+4 区域截图,截图会自动保存在剪切板中,然后你可以通过comman ...

  6. JAVA_MyEclipse常见配置NETGEAR路由器如何设置

    1 把宽带网线接到路由器的Internet口,然后用一个网线连接PC和路由器的任意端口(1,2,3,4一般四个),如下图所示 2 在浏览器地址栏中输入下面的网址,中途会弹出对话框询问账号密码,默认的账 ...

  7. LeetCode_3Sum

    一.题目 3Sum Total Accepted: 45112 Total Submissions: 267165My Submissions Given an array S of n intege ...

  8. 关于 underscore 中模板引擎的应用演示样例

    //关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset=" ...

  9. Jupyter notebook 使用Turorial

    The cell magics in IPython http://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/note ...

  10. Arcgis Engine(ae)接口详解(4):featureClass的feature插入

    //由于测试数据不完善,featureClass在此要只设null值,真实功能要设实际的值 IFeatureClass featureClass = null; //获取某个字段的索引,后面取字段值用 ...