在Service中使用广播接受者
1、清单文件
<service android:name="com.example.callmethod.MyService"></service>
2、开启服务和发送广播
package com.example.callmethod; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyService.class);
startService(intent);
} public void call(View view){
//发送广播
Intent intent = new Intent();
intent.setAction("com.example.callMethod");
sendBroadcast(intent);
} }
3、实现Service
package com.example.callmethod; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast; public class MyService extends Service { private MyRecervice recervice;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
//采用代码的方式实现广播接受者,所以不需要再清单文件中进行配置
recervice = new MyRecervice();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.callMethod");//说明它接收的action
registerReceiver(recervice, filter);
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(recervice);
recervice = null;
super.onDestroy();
} public void methodInService(){
Toast.makeText(this, "我是服务的方法", 0).show();
} private class MyRecervice extends BroadcastReceiver{
//当接收到广播后,调用的方法
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("我是服务内部的广播接收者,接收了广播事件");
methodInService();
} } }
在Service中使用广播接受者的更多相关文章
- BroadcastRecevier广播接受者
广播接收器的两种注册方式: 1)动态注册:在代码中注册,创建一个IntentFilter(意图过滤器)对象,设置想要就收的广播,在onCreate()方法中通过调用registerReceiver() ...
- BroadcastReceiver广播接受者
广播接收器的两种注册方式: 1)动态注册:在代码中注册,创建一个IntentFilter(意图过滤器)对象,设置想要就收的广播,在onCreate()方法中通过调用registerReceiver() ...
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...
- android84 广播接受者
#广播接收者(广播接受者进程关闭了也能接收到广播,系统会在清单文件中找哪个广播接受者可以收到这条广播,然后去启动这个接受者的进程,找不到则广播发了就发了没人收到而已) * 现实中:电台要发布消息,通过 ...
- Android中的广播
Android中的广播 广播接受器,可以比喻成收音机.而广播则可以看成电台. Android系统内部相当于已经有一个电台 定义了好多的广播事件,比如外拨电话 短信到来 sd卡状态 电池电量变化... ...
- Android初级教程理论知识(第六章广播接受者)
总体概述: 广播接收者 现实中:电台要发布消息,通过广播把消息广播出去,使用收音机,就可以收听广播,得知这条消息 Android中:系统在运行过程中,会产生很多事件,那么某些事件产生时,比如:电量改变 ...
- Android 中的广播(Broadcast)
Android 广播(broadcast) 饮水思源 本文章内容学习和总结自 郭霖大神:<Android第一行代码> Overview 就像我们的学校里的喇叭一样,是用来通知的.而Andr ...
- android 注册广播接受者
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 动态注册 静态注册 动态注册是 通过java代码,注册. 静态注册 是xml清单文件中 ...
- android中activity向service中传值
和activity中互相传值类似 在activity中 Intent regIntent = new Intent(this, ChatService.class); regIntent.putEx ...
随机推荐
- 配置分割Tomcat日志
安装cronolog 官网下载:http://cronolog.org/usage.html ./configuremake;make install 或者 yum install cronolog ...
- Python之路:Python 函数
一.函数式编程:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装 二. 函数的定义和使用 def 函数名(参数): ... 函数体 ... 函数的定义主要有 ...
- C# 一些小东东
在C#中,如果有一个方法我们不想继续使用,需要废弃的时候,可以在该方法前面加上一个[Obsolete]. string[] arr={"a","b"} if $ ...
- DataReader的例子
前: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataReader对 ...
- android应用Dialog跳转到Activity
public void back(Context context){ intent = new Intent(); intent.setClass(context, IBookActivity.cla ...
- cocos2dx进阶学习之坐标转换
在cocos2dx中,有四种坐标系 GL坐标系:左下为原点,x轴向右,y轴向上 UI坐标系:左上为原点,x轴向右,y轴向下 世界坐标系:与GL坐标系相同 本地坐标系:是节点(CCNode)的坐标系,原 ...
- git pull 出错 fatal: Could not read from remote repository.Please make sure you have the correct access rights.and the repository exists.
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hos ...
- android面试题之三
十一.对一些资源以及状态的操作保存,最好是保存在生命周期的哪个函数中进行? 若在onPause()中进行保存,相应需要在onResume()中进行恢复. 若在onStop()中进行保存,相应需要在on ...
- jbpm系列之五--使用decision节点判断分支情况
我们在用JBPM做流程的时候,很多时候会遇到需要判断的节点.类似java中的switch,根据不同的状态,跳转到不同的节点. 首先我们定义一个流程信息,jpdl流程图如下 明显的可以看到,在此种情况下 ...
- X Window System介绍
1.概述 X Window System是1984年由麻省理工学院(MIT)和DEC公司共同开发研究的,是执行在UNIX系统上的视窗系统.严格地说,X Window System并非一个软件, ...