写在前面:仅供参考,一般是自己留着看的,如有错误,敬请指正。
这次的更新接着上次的博客,智能移动设备软件开发复习-第三天
第6章 BroadcastReceiver(广播接收者)
- 广播接收者的创建
public class MyReceiver extends BroadcastReceiver{ } @Override public void onReceive(Context context, Intent intent) { throw new UnsupportedOperationException("Not yet implemented"); }
- 广播接收者的注册
- 静态注册
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true" > </receiver>
- 动态注册
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //实例化广播接收者 MyReceiver receiver = new MyReceiver(); String action = "action"; //action要与发送时的action一致 IntentFilter intentFilter = new IntentFilter(action); registerReceiver(receiver,intentfilter); } @Override protected void onDestroy(){ super.onDestroy(); unregisterReceiver(receiver); }
- 静态注册
- 自定义广播的发送与接收
- 自定义广播的发送
public void send(View view){ Intent intent = new Intent(); Intent.setAction("Help_stitch"); //发送广播 sendBroadcast(intent); }
- 自定义广播的接收
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context.Intent intent){ Log.i("MyBroadcastyReceiver", "自定义的广播接收者,接收到了求救广播事件"); Log.i("MyBroadcastReceiver", intent.getAction()); } }
<action android:name="Help_Stitch"/>
- 自定义广播的发送
- 有序广播与无序广播
- 无序广播
无序广播效率高,但无法被拦截sendBroadcast(intent);
- 有序广播
有序广播效率低,但此类型是有先后顺序的,可以被拦截intent.setAction("Intercept_Stitch"); sendOrderedBroadcast(intent,null);
- 拦截有序广播
abortBroadcast();//拦截有序广播
- 无序广播
第7章 Service(服务)
- 服务的创建
public class Myservice extends Service{ public Myservice(){ } @Override public IBinder onBind(Intent intent){ Log.i("EXAMPLE","--OnBind()"); return new CountBinder(); } }
- 服务的生命周期
- onCreate:第一次创建服务时执行的方法
- onDestroy():服务被销毁时执行的方法;
- onStartCommand:客户端通过调用startService()方法启动服务时执行该方法;
- onBind:客户端通过调用BindService()方法启动服务时执行该方法;
- onUnbind():客户端调用unBindService()断开服务绑定时执行的方法。
通过startService()方法启动服务时,执行的生命周期方法为onCreate()、onStartCommand(),然后服务处于运行状态,知道自身调用stopself()方法或者其他组件调用stopService()方法时服务停止,最终被系统销毁。
当使用bindService()方法启动服务时,执行的生命周期方法为onCreate()、onBind(),然后服务处于运行状态,直到调用unBindService()方法时,服务被解绑调用onUnbind()方法最终被销毁。
- 服务的启动方式
- startService方式启动
注:startService开启服务以后,与activity就没有关联,不受影响,独立运行。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); }
- bindService方式启动
注: bindService开启服务以后,与activity存在关联,退出activity时必须调用unbindService方法,否则会报ServiceConnection泄漏的错误。public class MainActivity extends AppCompatActivity{ private MyService.MyBinder myBinder; private Myconn myconn; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Bind(View view){ if(myconn == null){ myconn = new Myconn(); } Intent intent = new Intent(this,MyService.class); bindService(intent, myconn, BIND_AUTO_CREATE); } public void onUnBind(View view){ if(myconn != null){ unBindService(myconn); myconn = null; } } public void Call(View view){ myBinder.callMethodInService(); } private class MyConn implements ServiceConnection{ public void onServiceConnected(ComponentName name,IBinder service){ myBinder = (MyService.MyBinder) service; Log.i(" MainActivity","服务成功绑定,内存地址为:" + myBinder.toString()); } public void onServiceDisconnected(ComponentName name){ } } } }
- startService方式启动