Я разрабатываю приложение, в котором состояние Wi-Fi отключается, когда состояние телефона ЗВОНИТ.
Мой код выглядит следующим образом:
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?
Пожалуйста, помогите мне найти решение.
Заранее спасибо, ребята.