public class MainActivity extends Activity {

     private Button bt ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.bt); IntentFilter filter = new IntentFilter("BT");
Receiver recerver = new Receiver();
getApplicationContext().registerReceiver(recerver, filter); bt.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent intent = new Intent("BT");
intent.setPackage(getPackageName());
Bundle bundle = new Bundle();
bundle.putString("say", "Hello,I'm activity!");
intent.putExtras(bundle);
sendBroadcast(intent);
}
});
} }

MainActivity

 public class Receiver extends BroadcastReceiver {

     public void onReceive(Context context, Intent intent) {
String bd=intent.getStringExtra("say");
Toast.makeText(context,bd+ "I'm Receiver.I will send Intent!!!", Toast.LENGTH_LONG).show();
intent.setAction("myservice");
context.startService(intent);
} }

Receiver

 public class MyService extends Service {

     @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} public void onCreate(){
Toast.makeText(MyService.this, "onCreate()", Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent,int startId){
Toast.makeText(MyService.this, "onStart()", Toast.LENGTH_LONG).show(); }
public void onDestroy(){
Toast.makeText(MyService.this, "onDestroy()", Toast.LENGTH_LONG).show();
}
}

MyService

     <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService">
<intent-filter >
<action android:name="myservice" />
</intent-filter>
</service>
</application>

application

Android 利用广播接收器启动服务的更多相关文章

  1. Android:广播接收器(BroadCastReceiver)要点随笔。

    @@@描述         广播接收器可以收到 Context.sendBroadcast或者Context.sendOrderedBroadcast发出的意图(intent).   @@@Local ...

  2. android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题

    最近在公司处理项目时,用到锁屏状态弹出activity进行提示,类似QQ消息弹屏提示的功能.当中用到了,假如该弹出activity已经位于锁屏界面外时,将不进行再次弹窗,而是发送广播进行通知数据更新, ...

  3. Android利用广播监听设备安装和卸载应用程序

    MainActivity如下: package cn.testappaddandremove; import android.os.Bundle; import android.app.Activit ...

  4. 利用广播调用后台服务方法并根据方法返回的内容更新UI

    一.UI布局代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  5. Android 在广播接收器中弹出对话框

    特别需要注意的几点如下: 需要设置AlertDialog的类型 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 2. 需要声明Window弹框的权限 < ...

  6. [android] 利用广播实现ip拨号

    广播接收者,broadcast receiver,安卓系统在使用时会产生很多的事件,比如:短信到来,电量低,拨打电话等等 界面布局,线性布局,EditText指定为电话号码,设置属性android:i ...

  7. Android利用广播监听设备网络连接(断网)的变化情况

    http://www.open-open.com/lib/view/open1379302453943.html

  8. Xamarin.Android广播接收器与绑定服务

    一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...

  9. 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)

    1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...

随机推荐

  1. 剑指offer :跳台阶

    这题之前刷leetcode也遇到过,感觉是跟斐波拉契差不多的题. 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 解 ...

  2. SQL Server 2008 存储过程示例

    出处:http://www.jb51.net/article/54730.htm --有输入参数的存储过程-- create proc GetComment (@commentid int) as s ...

  3. Unity删除或更换天空盒

    Unity版本:5.6.2 点击菜单栏Window-->Lighting-->Settings 在弹出的窗口中,设置Skybox Material选项,从原来的默认天空盒更换成别的,或者选 ...

  4. 结对&团队之1715|K班取经

    声明:同学请勿抄袭,追责莫要怪我: 因为暂时闲着没事,就翻阅学长学姐的博客找找灵感,个人觉得应该还有人和我一样对软工实践未来的一大段路还很天真,包括目前的结对作业和团队组队也很迷路,于是写下这篇博客提 ...

  5. java中的Serializable接口的作用

    实现java.io.Serializable 接口的类是可序列化的.没有实现此接口的类将不能使它们的任一状态被序列化或逆序列化. 序列化类的所有子类本身都是可序列化的.这个序列化接口没有任何方法和域, ...

  6. web.config文件详解[转]

    一).Web.Config是以XML文件规范存储,配置文件分为以下格式1.配置节处理程序声明特点: 位于配置文件的顶部,包含在<configSections>标志中.2.特定应用程序配置特 ...

  7. JavaScript的setTimeout()和setInterval()

    1. setTimeout()方法 作用:在制定的毫秒数后调用函数或计算表达式 语法: setTimeout(code,millisec) 实例: function timedMsg() { var ...

  8. Python 变量 (上)

    Python通过变量引用内存中的值,变量的值占用多少空间是由变量的类型决定的.声明变量不需要指定变量的类型,解释器会自动根据值来判断.变量名称必须符合标识符的定义 标识符 标识符是由字母,数字和下划线 ...

  9. ansible操作(一)

    ansible晋级操作之ad-hoc命令 所谓的ad-hoc命令! 如果我们敲入一些命令去比较快的完成一些事情,而不需要将这些执行的命令特别保存下来, 这样的命令就叫做 ad-hoc 命令.Ansib ...

  10. 再谈 最速下降法/梯度法/Steepest Descent

    转载请注明出处:http://www.codelast.com/ 最速下降法(又称梯度法,或Steepest Descent),是无约束最优化领域中最简单的算法,单独就这种算法来看,属于早就“过时”了 ...