在RINGING手机状态下关闭wifi

时间:2011-11-22 11:33:27

标签: android android-widget android-manifest

我正在开发一个应用程序,当手机状态响起时,wifi状态会变为OFF状态。

我的代码如下:

phonestateManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phone.state"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application android:icon="@drawable/ic_launcher" >
        <activity
            android:label="@string/app_name"
            android:name=".PhonestateActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />    
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </activity>    
        <activity android:name=".WifitoggleActivity" />    
        <receiver android:name=".ServiceReceiver" />
    </application>    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />    
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    
    <uses-permission android:name="android.permission.WAKE_LOCK" />    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>

PhonestateActivity.java

public class PhonestateActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(PhonestateActivity.this,
                        ServiceReceiver.class);
                startActivity(intent);
            }
        });
    }
}

ServiceReceiver.java

public class ServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager);
        context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, MyPhoneStateListener.LISTEN_CALL_STATE);
    }
}

MyPhoneStateListener.java

public class MyPhoneStateListener extends PhoneStateListener {
    @SuppressWarnings("unused")
    private WifitoggleActivity ss;

    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            break;
            case TelephonyManager.CALL_STATE_RINGING: {
            ss = new WifitoggleActivity();
            Log.d("DEBUG", "RINGING");
            break;
            }
        }
    }
}

WifitoggleActivity.java

public class WifitoggleActivity extends Activity {
    public WifitoggleActivity() {
        System.out.print("INSIDE WIFI");
    }

    /** Called when the activity is first created. */
    private WifiManager wifiManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        } else {
            wifiManager.setWifiEnabled(true);
        }
    }
}

代码只是在我的设备中强制关闭。它是Manifest文件的问题吗?

请帮我找一个解决方案。

先谢谢你们。

1 个答案:

答案 0 :(得分:3)

ServiceReceiver是BroadcastReceiver而不是Activity。您在第一个块中使用了错误的代码。

在代码中找到onClick处理程序:

public class PhonestateActivity extends Activity {

     /** Called when the activity is first created. */   
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Button button1= (Button)findViewById(R.id.button1);
      button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

然后删除这两行:

        Intent intent = new Intent(PhonestateActivity.this,ServiceReceiver.class);
        startActivity(intent);

并将其替换为

        IntentFilter intentFilter=new IntentFilter(Intent.ACTION_ANSWER);
        registerReceiver(new ServiceReceiver(),intentFilter);

由于ServiceReceiver不是活动。

<强>被修改

再添加一项权限:

 android.permission.PROCESS_OUTGOING_CALLS

并更改

  <receiver android:name=".ServiceReceiver">
   <intent-filter>
     <action android:name=" android.intent.action.PHONE_STATE" />  
   </intent-filter>
  </receiver>

现在删除从oncreate方法注册广播的两行 Check this link