歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android settings模塊中添加來電反轉靜音功能

最近由於項目需要,添加來點Android反轉靜音功能,先將代碼寫出來,往大家給點意見

1.首先是一個主要的服務類,service

package com.create.wxb;

import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class TurnSilentService extends Service {
   
    private TelephonyManager telephonyMgr;
    private SensorManager sensorMgr;
    private AudioManager audioMgr;
    private SensorEventListener sensorListener;
    private PhoneCallListener phoneListener;
    private boolean ringing = false;
   
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Sensor orientation = null;
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        telephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
        audioMgr = (AudioManager)this.getSystemService(AUDIO_SERVICE);
        phoneListener = new PhoneCallListener();
        telephonyMgr.listen(phoneListener, PhoneCallListener.LISTEN_CALL_STATE);
        sensorListener = new SensorListener();
        List<Sensor> sensorList = sensorMgr.getSensorList(Sensor.TYPE_ALL);       
        int size = sensorList.size();
        for (int i = 0; i<size ;i++) {           
            int type = sensorList.get(i).getType();
            if (type == Sensor.TYPE_ACCELEROMETER) {
                orientation = sensorList.get(i);
                break;
            }
        }
        if (orientation != null) {
            sensorMgr.registerListener(sensorListener,orientation, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            stopSelf();
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        telephonyMgr.listen(phoneListener, 0);
        sensorMgr.unregisterListener(sensorListener);
    }
   
   
    private class PhoneCallListener extends PhoneStateListener{
          @Override
          public void onCallStateChanged(int state, String incomingNumber){
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE://電話處於待機狀態
              ringing = false;
              break;
            case TelephonyManager.CALL_STATE_OFFHOOK://電話通話中
              ringing = false;
              break;
            case TelephonyManager.CALL_STATE_RINGING://有電話來電
              ringing = true;
              break;
            }  
            super.onCallStateChanged(state, incomingNumber);
          }
        }


        class SensorListener implements SensorEventListener{
          @Override
          public void onAccuracyChanged(Sensor sensor, int accuracy) {}      
         
          @Override
          public void onSensorChanged(SensorEvent event) {    
            if (event.values[2] < 0 && ringing) {//電話正在響鈴中,且手機面朝下
              int ringerMode = AudioManager.RINGER_MODE_SILENT;
              audioMgr.setRingerMode(ringerMode);
            }else{
              audioMgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            }
          }
        }
   

}

上面這個service主要負責監聽手機來點,並實現反轉靜音功能,現在主要需要一個Activity 來啟動這個service。

2.activity來啟動service

package com.create.wxb;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.startService(new Intent("com.android.create.turnslicentservice"));
    }
}

3.需要在androidManifest.xml 文件中聲明activity和service

其中裡面是這樣寫的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.create.wxb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HelloWorldActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <service android:name=".TurnSilentService">
            <intent-filter >
                <action android:name="com.android.create.turnslicentservice" />
            </intent-filter>
         </service>
         <receiver android:name=".TurnSilentBroadcastReceiver">
             <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
         </receiver>
             
    </application>
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>

其中大家看到了,裡面有對電話狀態和系統啟動的使用權限

4.後面的那個TurnSilentBroadcastReceiver,這個主要是對系統氣的監聽,目的是讓系統啟動後是否對這個service進行啟動。

package com.create.wxb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class TurnSilentBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
       
        if (action.equals("android.intent.action.BOOT_COMPLETED")) {

           
        }

    }

}

以上是對上述功能的主要實現。

總體要在settings裡面內置的話,我們要考慮對是否開啟反轉靜音的狀態保存。具體的實現在這就不寫了,

謝謝

Copyright © Linux教程網 All Rights Reserved