Android 启动Service服务和发送Broadcast广播的常用方法

一、先说Service服务。

  1、利用setAction()方法来指定启动的Service服务

1 Intent intent = new Intent();
2 intent.setAction("ServiceAction");
3 startService(intent);

  2、使用Intent的构造函数类添加Activity内容

1 Intent intent = new Intent("ServiceAction");
2 startService(intent);

  3、和Activity之间跳转类似

1 Intent intent = new Intent(MainActivity.this,ServiceTest.class);
2 startService(intent);

二、再看Broadcast广播

  1、利用setAction()方法来指定发送的Broadcast广播

1 Intent intent = new Intent();
2 intent.setAction("BroadcastAction");
3 sendBroadcast(intent);

  2、使用Intent的构造函数类添加Activity内容

1 Intent intent = new Intent("BroadcastAction");
2 sendBroadcast(intent);

  3、和Activity之间跳转类似

1 Intent intent = new Intent(MainActivity.this,BroadcastTest.class);
2 sendBroadcast(intent);

 

posted @ 2015-05-26 10:27  小寻  阅读(2414)  评论(0编辑  收藏  举报