我正在尝试设置检测屏幕何时关闭并禁用wifi的服务。
这是我的代码:
服务:
public static boolean isScreen;
private int mInterval = 5000;
private Handler mHandler;
@Override
public void onCreate() {
mHandler = new Handler();
startRepeatingTask();
IntentFilter mFilter = new IntentFilter("com.my.package.myAction");
BroadcastReceiver mReceiver = new screenReceiver();
registerReceiver(mReceiver, mFilter);
}
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction("com.my.package.myAction");
sendBroadcast(intent);
PowerManager pm = (PowerManager) androidTweaker.this.getSystemService(Context.POWER_SERVICE);
isScreen = pm.isScreenOn();
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
这是我的第一个问题:我设置了一个处理程序,每5秒检查一次屏幕的状态。你有更好的解决方案吗?
我的接收者:
public class screenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!myService.isScreen && !mWifi.isConnected()) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
}
这里我的服务崩溃了。 logcat报告此行中的错误:wifiManager.setWifiEnabled(false); (接收器)
我的清单:
<service android:name=".myService"/>
<receiver android:name=".screenReceiver">
<intent-filter>
<action android:name="com.my.package.myAction"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<user-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
我的服务当然得到了前景通知,因此它始终开启。 有什么帮助吗?
修改 我得到了替换
解决的主要问题(改变wifi状态)<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<user-permission android:name="android.permission.CHANGE_WIFI_STATE" />
与
<use**s**-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<use**s**-permission android:name="android.permission.CHANGE_WIFI_STATE" />
任何方法让服务检测屏幕没有处理程序?
EDIT2:问题1也解决了:
服务:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction("com.my.package.myAction");
BroadcastReceiver mReceiver = new screenReceiver();
registerReceiver(mReceiver, filter);
接收器:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
Intent i = new Intent(context, androidTweaker.class);
context.startService(i);
}
答案 0 :(得分:1)
您需要在清单接收器中添加以下行以进行wifi状态更改:
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
希望这有帮助。
答案 1 :(得分:1)
要获得屏幕的开/关状态,请使用ACTION_SCREEN_ON和ACTION_SCREEN_OFF意图。