在 RINGING 手机状态下关闭 wifi
Posted
技术标签:
【中文标题】在 RINGING 手机状态下关闭 wifi【英文标题】:Turn off wifi on RINGING phone state 【发布时间】:2012-01-03 19:06:55 【问题描述】:我正在开发一个应用程序,当手机状态为 RINGING 时,wifi 状态会关闭。
我的代码如下:
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 文件有问题吗?
请帮我找到解决办法。
提前谢谢各位。
【问题讨论】:
如果您需要指导,那么我们随时为您提供帮助。没有人有时间阅读所有这些代码。请具体说明。您是社区的新手,因此请记住未来。我也会对您有所帮助。可以你张贴你的日志猫 当然,先生.........正如你所说,我是这个社区的新手......我会发布我的 logcat 张贴你的日志猫,这将有助于轻松识别错误 11-22 16:52:41.200: D/AndroidRuntime(270): 关闭 VM 11-22 16:52:41.200: W/dalvikvm(270): threadid=1: 线程退出未捕获的异常 (group=0x4001d800) 11-22 16:52:41.229: E/AndroidRuntime(270): 致命异常: main 11-22 16:52:41.229: E/AndroidRuntime(270): android.content.ActivityNotFoundException: Unable查找明确的活动类 com.phone.state/com.phone.state.ServiceReceiver;您是否在 AndroidManifest.xml 中声明了此活动? 11-22 16:52:41.229: E/AndroidRuntime(270): 在 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404) 11-22 16:52:41.229: E/AndroidRuntime(270): 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 11-22 16:52:41.229: E/ AndroidRuntime(270): 在 android.app.Activity.startActivityForResult(Activity.java:2817) 11-22 16:52:41.229: E/AndroidRuntime(270): 在 android.app.Activity.startActivity(Activity.java:2923 ) 11-22 16:52:41.229: E/AndroidRuntime(270): 在 com.phone.state.PhonestateActivity$1.onClick(PhonestateActivity.java:22) 11-22 16:52:41.229: E/AndroidRuntime(270) :在 android.view.View.performClick(View.java:2408) 【参考方案1】: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
【讨论】:
是的,我改变了,但应用程序直接发送广播,没有 UI...你可以检查清单文件,如果权限有任何不匹配......以上是关于在 RINGING 手机状态下关闭 wifi的主要内容,如果未能解决你的问题,请参考以下文章
广播接收器调用两次EXTRA_STATE_RINGING状态,导致不变数据
手机进入睡眠状态后,在前台服务中保持 wifi 处于活动状态