敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述android 学习随笔十八(广播与服务 )相关的知识,希望能为你提供帮助。
1、广播接收者注册
- 清单文件注册(Android四大组件都要在清单文件中注册)
- 广播接收者可以使用代码注册
特殊广播接收者,必须代码注册。电量改变、屏幕开关,必须使用代码注册
2、注册广播接收者
//创建广播接收者对象
receiver = new ScreenOnOffReceiver();
//通过IntentFilter对象指定广播接收者接收什么类型的广播
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
//注册广播接收者
registerReceiver(receiver, filter);
* 解除注册广播接收者
unregisterReceiver(receiver);
* 解除注册之后,广播接收者将失去作用
文章图片
文章图片
1 package com.itheima.register; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 7 public class ScreenOnOFFReceiver extends BroadcastReceiver { 8 9@Override 10public void onReceive(Context context, Intent intent) { 11 12String action = intent.getAction(); 13if(Intent.ACTION_SCREEN_ON.equals(action)){ 14System.out.println("屏幕开啦啦"); 15} 16else if(Intent.ACTION_SCREEN_OFF.equals(action)){ 17System.out.println("屏幕关哇哇"); 18} 19 20} 21 22 }
View Code
文章图片
文章图片
1 package com.itheima.register; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.content.IntentFilter; 6 import android.os.IBinder; 7 8 public class RegisterService extends Service { 9 10private ScreenOnOFFReceiver receiver; 11 12@Override 13public IBinder onBind(Intent intent) { 14// TODO Auto-generated method stub 15return null; 16} 17 18@Override 19public void onCreate() { 20super.onCreate(); 21//1.创建广播接收者对象 22receiver = new ScreenOnOFFReceiver(); 23//2.创建意图过滤器对象 24IntentFilter filter = new IntentFilter(); 25//指定接收什么广播 26filter.addAction(Intent.ACTION_SCREEN_OFF); 27filter.addAction(Intent.ACTION_SCREEN_ON); 28//3.注册广播接收者 29registerReceiver(receiver, filter); 30 31} 32 33@Override 34public void onDestroy() { 35super.onDestroy(); 36//反注册广播接收者 37unregisterReceiver(receiver); 38} 39 40 }
View Code
文章图片
文章图片
1 package com.itheima.register; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 import android.view.View; 8 9 public class MainActivity extends Activity { 10 11@Override 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14setContentView(R.layout.activity_main); 15} 16 17 18public void start(View v){ 19Intent intent = new Intent(this, RegisterService.class); 20startService(intent); 21} 22public void stop(View v){ 23Intent intent = new Intent(this, RegisterService.class); 24stopService(intent); 25} 26 27 }
View Code 服务注册广播
3、服务的分类
- 本地服务:服务和启动它的组件在同一个进程
- 远程服务:服务和启动它的组件不在同一个进程
- 远程服务只能隐式启动,类似隐式启动Activity,在清单文件中配置Service标签时,必须配置intent-filter子节点,并指定action子节点
文章图片
文章图片
1 package com.itheima.remoteservice; 2 3 import com.itheima.remoteservice.PublicBusiness.Stub; 4 5 import android.app.Service; 6 import android.content.Intent; 7 import android.os.Binder; 8 import android.os.IBinder; 9 10 public class RemoteService extends Service{ 11 12@Override 13public IBinder onBind(Intent intent) { 14System.out.println("绑定"); 15return new FengMiShu(); 16} 17 18class FengMiShu extends Stub{ 19 20@Override 21public void qianXian() { 22// TODO Auto-generated method stub 23remoteBanZheng(); 24} 25 26} 27 28@Override 29public boolean onUnbind(Intent intent) { 30System.out.println("解绑"); 31return super.onUnbind(intent); 32} 33 34@Override 35public void onCreate() { 36System.out.println("创建"); 37super.onCreate(); 38} 39 40@Override 41public int onStartCommand(Intent intent, int flags, int startId) { 42System.out.println("开始"); 43return super.onStartCommand(intent, flags, startId); 44} 45 46@Override 47public void onDestroy() { 48System.out.println("摧毁"); 49super.onDestroy(); 50} 51 52public void remoteBanZheng(){ 53System.out.println("苗领导在国外远程办证"); 54} 55 56 }
View Code
文章图片
文章图片
1 package com.itheima.remoteservice; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 public class MainActivity extends Activity { 8 9@Override 10protected void onCreate(Bundle savedInstanceState) { 11super.onCreate(savedInstanceState); 12setContentView(R.layout.activity_main); 13} 14 15 16@Override 17public boolean onCreateOptionsMenu(Menu menu) { 18// Inflate the menu; this adds items to the action bar if it is present. 19getMenuInflater().inflate(R.menu.main, menu); 20return true; 21} 22 23 }
View Code
文章图片
文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.itheima.remoteservice" 4android:versionCode="1" 5android:versionName="1.0" > 6 7< uses-sdk 8android:minSdkVersion="8" 9android:targetSdkVersion="17" /> 10 11< application 12android:allowBackup="true" 13android:icon="@drawable/ic_launcher" 14android:label="@string/app_name" 15android:theme="@style/AppTheme" > 16< activity 17android:name="com.itheima.remoteservice.MainActivity" 18android:label="@string/app_name" > 19< intent-filter> 20< action android:name="android.intent.action.MAIN" /> 21 22< category android:name="android.intent.category.LAUNCHER" /> 23< /intent-filter> 24< /activity> 25< service android:name="com.itheima.remoteservice.RemoteService"> 26< intent-filter > 27< action android:name="com.itheima.remote"/> 28< /intent-filter> 29< /service> 30< /application> 31 32 < /manifest>
View Code调用远程服务操作
package com.itheima.runremoteservice; public interface PublicBusiness {void qianXian(); }
文章图片
文章图片
1 package com.itheima.runremoteservice; 2 3 import android.os.Bundle; 4 import android.os.IBinder; 5 import android.app.Activity; 6 import android.content.ComponentName; 7 import android.content.Intent; 8 import android.content.ServiceConnection; 9 import android.view.Menu; 10 import android.view.View; 11 12 public class MainActivity extends Activity { 13 14public PublicBusiness pb; 15private MyConn conn; 16 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21 22conn = new MyConn(); 23} 24 25 26public void start(View v){ 27Intent intent = new Intent(); 28intent.setAction("com.itheima.remote"); 29startService(intent); 30} 31public void stop(View v){ 32Intent intent = new Intent(); 33intent.setAction("com.itheima.remote"); 34stopService(intent); 35} 36public void bind(View v){ 37Intent intent = new Intent(); 38intent.setAction("com.itheima.remote"); 39bindService(intent, conn, BIND_AUTO_CREATE); 40} 41 42public void unbind(View v){ 43unbindService(conn); 44} 45class MyConn implements ServiceConnection{ 46 47@Override 48public void onServiceConnected(ComponentName name, IBinder service) { 49pb = (PublicBusiness) service; 50 51} 52 53@Override 54public void onServiceDisconnected(ComponentName name) { 55// TODO Auto-generated method stub 56 57} 58 59} 60 61public void banZheng(View v){ 62pb.qianXian(); 63} 64 }
View Code
4、服务的分类
才
【android 学习随笔十八(广播与服务 )】
推荐阅读
- android 获取512*512规格的图片
- Android向系统相册中插入图片,相册中会出现两张一样的图片(只是图片大小不一致)
- Android开发游记(RecycleView 实现复杂首页布局三种方式)
- Centos 7 使用jenkins 打包android项目
- Android开篇(转)
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- android-ListView空间的使用
- Android 图片缩放
- Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)