Android 利用广播接收器启动服务

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 利用广播接收器启动服务的更多相关文章
- Android:广播接收器(BroadCastReceiver)要点随笔。
@@@描述 广播接收器可以收到 Context.sendBroadcast或者Context.sendOrderedBroadcast发出的意图(intent). @@@Local ...
- android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题
最近在公司处理项目时,用到锁屏状态弹出activity进行提示,类似QQ消息弹屏提示的功能.当中用到了,假如该弹出activity已经位于锁屏界面外时,将不进行再次弹窗,而是发送广播进行通知数据更新, ...
- Android利用广播监听设备安装和卸载应用程序
MainActivity如下: package cn.testappaddandremove; import android.os.Bundle; import android.app.Activit ...
- 利用广播调用后台服务方法并根据方法返回的内容更新UI
一.UI布局代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- Android 在广播接收器中弹出对话框
特别需要注意的几点如下: 需要设置AlertDialog的类型 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 2. 需要声明Window弹框的权限 < ...
- [android] 利用广播实现ip拨号
广播接收者,broadcast receiver,安卓系统在使用时会产生很多的事件,比如:短信到来,电量低,拨打电话等等 界面布局,线性布局,EditText指定为电话号码,设置属性android:i ...
- Android利用广播监听设备网络连接(断网)的变化情况
http://www.open-open.com/lib/view/open1379302453943.html
- Xamarin.Android广播接收器与绑定服务
一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...
随机推荐
- sql经常出现死锁解决办法
文章:sql server在高并发状态下同时执行查询与更新操作时的死锁问题
- DPDK flow_filtering 源码阅读
代码部分 main.c /*- * BSD LICENSE * * Copyright 2017 Mellanox. * * Redistribution and use in source and ...
- ASP.NET MVC 3.0 参考源码索引
http://www.projky.com/asp.netmvc/3.0/Microsoft/Internal/Web/Utils/CommonResources.cs.htmlhttp://www. ...
- mysql只修改年月日,时分秒不变
比如时间为 “2014-12-11 13:12:01”修改为“2014-09-26 13:12::01”修改内容为修改yyyy-mm-dd为对应的日期,时间不变.UPDATE result_road_ ...
- pygame学习笔记(3)——时间、事件、文字
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率 上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...
- error_reporting 报错
<?php // 关闭所有PHP错误报告 error_reporting(0); // 报告简单的运行错误 error_reporting(E_ERROR | E_WARNING | E_PAR ...
- redis哨兵机制二(转)
概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如 master宕机了,Redis本身(包括它的很多客户端) ...
- httpstat的简单使用
httpstat 应该是一个 python 封装后的 curl 工具能够展现 一些客户端连接网站的时间消耗,最近在看tls 感觉挺有用处的 简单学习一下 1. centos7 安装python 和 p ...
- elasticsearch6 学习之数据分片
ES: ElasticSearch(简称ES):是一个基于Lucene构建的开源.分布式.RESTful的全文本搜索引擎:它还是一个分布式实时文档存储,其中每个field均是被索引的数据且可被搜索:也 ...
- js & get recursive ids
js & get recursive ids len = 0; bug for(let i = 0; i < 3; i++) { console.log(`i =`, i); let y ...