发送广播
Intent intent = new Intent("com.example.action.send");
sendBroadcast(intent);
接收广播
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("meeeeeee", "播放音乐");
}
}
配置,不需要category
<receiver android:name="com.example.MusicReceiver">
<intent-filter >
<action android:name="com.example.action.send"/>
</intent-filter>
</receiver>
发送有序广播
public void send(View v){
Intent intent = new Intent("com.example.receiver03.action.PASSGAME");
sendOrderedBroadcast(intent,
null,
null,
null,
0,
"hh",
null);
}
在配置文件中设置优先级
<receiver android:name="com.example.q134.PlayerReceiver">
<intent-filter android:priority="111">
<action android:name="com.example.receiver03.action.PASSGAME"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.q134.PlayerReceiverB">
<intent-filter android:priority="555">
<action android:name="com.example.receiver03.action.PASSGAME"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.q134.PlayerReceiverC">
<intent-filter android:priority="888">
<action android:name="com.example.receiver03.action.PASSGAME"/>
</intent-filter>
</receiver>
服务
电话窃听(4.4版本以上无效)
public class PhoneService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.v("meeeeeee", "开启服务");
TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
manager.listen(new PhoneStateListener(){
String Pnumber = "";
boolean isCalling =false;
private MediaRecorder mMediaRecorder;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("meeeeeee", "待机状态");
if (isCalling) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder=null;
isCalling=false;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if(isCalling){
/**录音
* 1.创建实例
* 2.音源是麦克风
* VOICE_DOWNLINK对方的声音
* 3.设置音频保存格式
* 4.指定保存位置
* 5.设置音频解码格式
* 6.设置音频编码
* */
try {
if (mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder();
mMediaRecorder
.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
mMediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(new File(
getFilesDir(), "a.3gp")
.getAbsolutePath());
}
mMediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.prepare();
} catch (Exception e) {
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
isCalling = true;
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
给上权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<service android:name="com.example.spythephone.PhoneService" >
</service>
服务的生命周期
startService(intent);
oncreate
onCommandStart
ondestroy
bindService(intent);
oncreate
onBind
onUnbind
onDestroy
AIDL
远程调用服务的Demo
服务端:
public class AlipayService extends Service {
private class Alipayer extends IAlipay.Stub {
@Override
public int callPay(String name, String pwd, String paypwd,
long currentTimeMiles, double money) {
return pay(name, pwd, paypwd,
currentTimeMiles, money);
}
}
@Override
public IBinder onBind(Intent intent) {
return new Alipayer();
}
public int pay(String name, String pwd, String paypwd,
long currentTimeMiles, double money) {
if (!name.equals("zhangsan") || !pwd.equals("123")
|| !paypwd.equals("123")) {
return 606;
}
if (money > 600) {
return 404;
}
return 200;
}
}
客户端:
将aidl文件复制到和服务端同名的包下
public void buy(View v){
Intent service=new Intent("com.example.alipay.START_ALIPAY");
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IAlipay agent = IAlipay.Stub.asInterface(service);
try {
int code = agent.callPay("zhangsan", "123", "123", System.currentTimeMillis(), 1200);
handlecode(code);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(service, conn, BIND_AUTO_CREATE);
}