采用广播的形式接间的调用服务中的方法
来源:互联网 发布:炒股软件ipad版 编辑:程序博客网 时间:2024/04/29 05:38
//服务public class MyService extends Service { private MyReceiver receiver; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { System.out.println("oncreate"); super.onCreate(); //采用代码的方式 来注册 广播 接受者 receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("com.chain.callmethod"); registerReceiver(receiver, filter); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onstartcommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { //服务销毁时, 取消广播接受者 unregisterReceiver(receiver); System.out.println("ondestory"); super.onDestroy(); } /** * 这是服务里面的一个方法 */ public void methodInService() { Toast.makeText(this, "哈哈,我是服务里面的方法", 0).show(); } //自定义广播接收者 private class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { methodInService();//代用服务中的方法 } }}
//activitypublic class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 作用: * 1.开启服务 * 2.让服务中的广播接受者就可以处理发出去的广播事件 * 3.广播接受者 根据处理的事件 来调用 服务中的 方法 * * @param view */ public void start(View view){ Intent intent = new Intent(this,MyService.class); //通知框架开启服务。 startService(intent); } public void stop(View view){ Intent intent = new Intent(this,MyService.class); stopService(intent); } @Override protected void onDestroy() { System.out.println("啊啊啊,我是activity,我挂了"); super.onDestroy(); } //调用服务里面的方法。不可以自己new服务,调用的服务的方法,必须通过框架得到服务的引用。 public void call(View view){ //发送一个自定义的广播 Intent intent = new Intent(); intent.setAction("com.chain.callmethod"); sendBroadcast(intent); }}
0 0
- 采用广播的形式接间的调用服务中的方法
- 利用广播调用服务里面的方法
- 利用广播调用 服务的方法
- Android 利用广播调用服务中的方法
- 代码注册广播接收者,利用广播调用服务中的方法
- Android -- service 利用广播调用服务的方法
- 代码注册广播接受者调用服务里面的方法
- android,利用广播接收者调用服务的方法,代码注册广播接收者
- 91_采用绑定服务的方式调用服务里面的方法
- android基础笔记:服务(Service) 采用接口的方式调用本地服务的方法
- vs2008中调用matlab,采用dll的形式
- 绑定的方式调用服务的方法和采用接口方式抽取方法
- 基于spring的调用web形式的cxf服务的方法
- 广播的两种形式
- php字符串形式调用类的方法
- android ,广播接受者。调用服务内部方法
- Ajax中的send采用的方法
- jsp调用applet以及采用js调用applet的方法
- 第一篇:51单片机的中断和定时
- 上海PPT
- Can't create table './store/#sql-b2c_1a.frm' (errno: 150)解决方法
- Service之跨进程调用服务ADIL详解(一)
- Activity的基本用法
- 采用广播的形式接间的调用服务中的方法
- hdu5301Buildings
- iOS之多线程---Runloop和多线程
- poj 1016水
- Android图片本地载入,显示绝对路线,并载入
- Jquery基础
- sup框架 控件的约束
- Android 网络编程
- 第一次使用Android Studio 下载安装设置(1)