一。服务的概念:
1.子线程:
new Thread(){}.start(); 子线程没有界面,也是长期后台运行的。
2.服务的特点:
android系统进程管理是按照一定的规则的:
1)应用程序一旦被打开 通常情况下关闭(清空任务栈)后进程不会停止。方面下一次快速启动。带来内存不足的问题。
2)Android系统有一套 内存清理机制。 按照优先级去回收系统的内存。
3.进程保留优先级:
进程分为5个等级的优先级:(从高到低)
1)Foreground process 前台进程 用户正在玩的应用程序对应的进程
2)Visible process 可视进程 用户仍然可以看到这个进程的界面。
3)Service process服务进程 应用程序有一个服务组件在后台运行。
4)Background process 后台进程 应用程序没有服务在运行 并且最小化 (activity onstop)
5)Empty process 空进程 没有任何运行的activity, 任务栈空了
4.注意点:
长期后台运行的组件, 不要在activity开启子线程。
应该是创建服务,在服务里面开启子线程。
5.服务的目的:
1)长期后台运行。
2)提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,把进程重新创建。
二。服务创建流程:
1.创建自定义服务类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// 服务的声明周期:oncreate,ondestory,onstart // 服务没有界面 public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { System.out.println("1.服务创建了"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("2.服务接收到了开启指令"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { System.out.println("3.服务销毁了"); super.onDestroy(); } } |
2.注册服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <manifest > <application > <activity android:name=".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.cuiweiyou.testservice.MyService"/> </application> </manifest> |
3.启动服务:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1.创建意图 Intent intent = new Intent(this, MyService.class); // 2.启动服务 startService(intent); } } |
三。电话监听器:
1.创建服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
/*** 一。创建电话监听服务 **/ public class MyTelephoneService extends Service { // 电话管理器 private TelephonyManager tm; // 监听器对象 private MyListener listener; // 使用自定义类 // 录音机 private MediaRecorder mediaRecorder; @Override public IBinder onBind(Intent intent) { return null; } /* 1.服务创建。初始化各种器具 */ @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.指定录音机的声音源(MIC只能录话筒的声音,全球有效。VOICE_CALL可录双方声音,但欧美电话不可用) 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(); /* // 流氓程序,服务守护方式1: // 服务不能启动自身,所以当本服务被终止时,启动其它具有相同功能的服务 Intent i = new Intent(this,MyTelephoneService2.class); // MyTelephoneService2:复制本MyTelephoneService.java,注册 startService(i); tm.listen(listener, PhoneStateListener.LISTEN_NONE); */ // 取消电话的监听 listener = null; } } |
2.注册服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="utf-8"?> <manifest > <!-- 二。增加权限 --> <!-- 1.获取电话状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 2.使用扩展设备,TF内存卡 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 3.录制音频 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <application > <activity android:name="com.cuiweiyou.vigilesphonelistener.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.cuiweiyou.vigilesphonelistener.MyTelephoneService" /> </application> </manifest> |
3.启动服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //开启服务。 Intent intent = new Intent(this,MyTelephoneService.class); startService(intent); //停止服务。即使停止了,当内存资源充足时,系统会自动启动服务 intent = new Intent(this,MyTelephoneService.class); stopService(intent); } } |
4.维持服务运行的其它方式:
1)创建一个广播接收者
1 2 3 4 5 6 7 8 9 10 11 |
/** 一旦开机,收到系统启动完毕广播,即启动电话监听服务 **/ public class BootCompletedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context,MyTelephoneService.class); context.startService(i); } } |
2)注册接收者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!-- 4.接收系统启动结束广播 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application > <!-- 广播接收者,收听系统启动状态 --> <receiver android:name="com.cuiweiyou.vigilesphonelistener.BootCompletedReceiver" > <intent-filter> <!-- 收听系统启动完成事件 再流氓一点,收听其它任何事件,如:拨出电话、打入电话号、来短信、发短信、拍照、锁屏、... --> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> |
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/277.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设