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);
* 解除注册之后,广播接收者将失去作用
package com.itheima.register; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class ScreenOnOFFReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) { String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action)){
System.out.println("屏幕开啦啦");
}
else if(Intent.ACTION_SCREEN_OFF.equals(action)){
System.out.println("屏幕关哇哇");
} } }
package com.itheima.register; import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder; public class RegisterService extends Service { private ScreenOnOFFReceiver receiver; @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
super.onCreate();
//1.创建广播接收者对象
receiver = new ScreenOnOFFReceiver();
//2.创建意图过滤器对象
IntentFilter filter = new IntentFilter();
//指定接收什么广播
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
//3.注册广播接收者
registerReceiver(receiver, filter); } @Override
public void onDestroy() {
super.onDestroy();
//反注册广播接收者
unregisterReceiver(receiver);
} }
package com.itheima.register; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void start(View v){
Intent intent = new Intent(this, RegisterService.class);
startService(intent);
}
public void stop(View v){
Intent intent = new Intent(this, RegisterService.class);
stopService(intent);
} }
服务注册广播
3、服务的分类
- 本地服务:服务和启动它的组件在同一个进程
- 远程服务:服务和启动它的组件不在同一个进程
- 远程服务只能隐式启动,类似隐式启动Activity,在清单文件中配置Service标签时,必须配置intent-filter子节点,并指定action子节点
远程服务
package com.itheima.remoteservice; import com.itheima.remoteservice.PublicBusiness.Stub; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class RemoteService extends Service{ @Override
public IBinder onBind(Intent intent) {
System.out.println("绑定");
return new FengMiShu();
} class FengMiShu extends Stub{ @Override
public void qianXian() {
// TODO Auto-generated method stub
remoteBanZheng();
} } @Override
public boolean onUnbind(Intent intent) {
System.out.println("解绑");
return super.onUnbind(intent);
} @Override
public void onCreate() {
System.out.println("创建");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("开始");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
System.out.println("摧毁");
super.onDestroy();
} public void remoteBanZheng(){
System.out.println("苗领导在国外远程办证");
} }
package com.itheima.remoteservice; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.remoteservice"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.remoteservice.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="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
</application> </manifest>
调用远程服务操作
package com.itheima.runremoteservice; public interface PublicBusiness { void qianXian();
}
package com.itheima.runremoteservice; import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { public PublicBusiness pb;
private MyConn conn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); conn = new MyConn();
} public void start(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
startService(intent);
}
public void stop(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
stopService(intent);
}
public void bind(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
} public void unbind(View v){
unbindService(conn);
}
class MyConn implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
pb = (PublicBusiness) service; } @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } } public void banZheng(View v){
pb.qianXian();
}
}
4、AIDL Android interface definition language,在Android中用于进程间通讯
* 安卓接口定义语言
* 作用:跨进程通信
* 应用场景:远程服务中的中间人对象,其他应用是拿不到的,那么在通过绑定服务获取中间人对象时,就无法强制转换,使用aidl,就可以在其他应用中拿到中间人类所实现的接口
步骤
远程服务
1. 把接口文件的后缀名改成aidl(在gen目录下会根据aidl文件生成一个.java文件)
2. aidl文件中所有东西都是public的,不需要也不能自己定义访问修饰符
3. 中间人对象继承Stub,这个对象已经继承了Binder并实现了PublicBusiness接口(自定义接口)
启动调用远程服务
1. 把远程服务项目的aidl文件复制到启动调用远程服务项目(先创建一个和服务aidl所在包相同的包名),然后aidl所在的包名远程服务和启动调用远程服务项目必须一致(在gen目录下会根据aidl文件生成一个.java文件)
2. 把启动调用远程服务项目获取到的中间人对象使用Stub.asInterface强转
public void onServiceConnected(ComponentName name, IBinder service) {
pb = Stub.asInterface(service);
}
例:
- 支付宝远程服务
1. 定义支付宝的服务,在服务中定义pay方法
2. 定义中间人对象,把pay方法抽取成接口
3. 把抽取出来的接口后缀名改成aidl
4. 中间人对象直接继承Stub对象
5. 注册这个支付宝服务,定义它的intent-Filter
- 需要支付的应用
1. 把刚才定义好的aidl文件拷贝过来,注意aidl文件所在的包名必须跟原包名一致
2. 远程绑定支付宝的服务,通过onServiceConnected方法我们可以拿到中间人对象
3. 把中间人对象通过Stub.asInterface方法强转成定义了pay方法的接口
4. 调用中间人的pay方法
package com.itheima.paybaby; import com.itheima.paybaby.PayInterface.Stub; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class PayService extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new PayController();
} class PayController extends Stub{ @Override
public void pay() throws RemoteException {
PayService.this.pay(); } } private void pay() {
System.out.println("检查支付环境");
System.out.println("加密账号密码");
System.out.println("上传账号密码");
System.out.println("完成支付"); }
}
package com.itheima.paybaby; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
调用
package com.itheima.naocangame; import com.itheima.paybaby.PayInterface;
import com.itheima.paybaby.PayInterface.Stub; import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { PayInterface pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //绑定远程服务
Intent intent = new Intent("com.itheima.babypay");
bindService(intent, new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
pi = Stub.asInterface(service); }
}, BIND_AUTO_CREATE); } public void click(View v){
try {
pi.pay();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
5、进程优先级
- 五种前台进程
1. activity执行了onresume方法,获得焦点
2. 拥有一个跟正在与用户交互的activity绑定的服务
3. 拥有一个服务执行了startForeground()方法
4. 拥有一个正在执行onCreate()、onStart()或者onDestroy()方法中的任意一个的服务
5. 拥有一个正在执行onReceive方法的广播接收者
- 两种可见进程
1. activity执行了onPause方法,失去焦点,但是可见
2. 拥有一个跟可见或前台activity绑定的服务