Android settings模块中添加来电反转静音功能(2)


        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 {

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pszgw.html