[英]Connect WiFi Manager from separate AsyncTask class in Android
I have an application that uses an AsyncTask
to connect to WiFi using WifiManager
. 我有一个使用
AsyncTask
使用WifiManager
连接到WiFi的应用程序。
Now, I am using this in my MainActivity
class as 现在,我在
MainActivity
类中将其用作
public class connectWifi extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
public connectWifi(MyActivity activity) {
dialog = new ProgressDialog(activity);
}
@Override
protected void onPreExecute() {
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Connecting to WiFi. Please Wait.");
dialog.show();
}
@Override
protected void onPostExecute(Void result) {
connected = true;
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo conn = wifi.getConnectionInfo();
Context context = getApplicationContext();
CharSequence wifi_on = "Connected to " + conn.getSSID();
int duration = Toast.LENGTH_LONG;
Toast responseToast = Toast.makeText(context, wifi_on, duration);
responseToast.show();
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
protected Void doInBackground(Void... voids) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
try {
Thread.sleep(3000);
} catch (Exception e) {
}
return null;
}
}
Now, I have another class AlarmReceiver
which gets called via a Broadcast
. 现在,我还有另一个类
AlarmReceiver
,它通过Broadcast
调用。 I am performing some network based action in that class which needs me to connect to WiFi first. 我正在该课程中执行一些基于网络的操作,需要我首先连接到WiFi。
How can I create a separate class for connectWifi
which does not involve first running MainActivity
? 我如何为
connectWifi
创建一个单独的类, connectWifi
不涉及首先运行MainActivity
?
The issue I am facing is in creating the Context
object which says Non-static 'getApplicationContext()' cannot be referenced from static context
. 我面临的问题是创建
Context
对象,该对象表示Non-static 'getApplicationContext()' cannot be referenced from static context
。
The same issue is with getSystemService()
. 同样的问题是
getSystemService()
。 How can I create this class as needed? 如何根据需要创建此类?
abc
is the AsyncTask
class. abc
是AsyncTask
类。
AlarmReceiver
is the BroadcastReceiver
AlarmReceiver
是BroadcastReceiver
My current flow of control is as follows: 我当前的控制流程如下:
START PROCEDURE 开始程序
- AlarmManager
receives Broadcast
AlarmManager
接收Broadcast
- AlarmManager
calls enableWifi()
in abc
AlarmManager
在abc
调用enableWifi()
- enableWifi()
connects to WiFi enableWifi()
连接到WiFi
- AlarmManager
checks for connection state AlarmManager
检查连接状态
- AlarmManager
then calls new abc().execute("DATA1")
AlarmManager
然后调用new abc().execute("DATA1")
- abc
then performs the doInBackground
task (network request) -然后
abc
执行doInBackground
任务(网络请求)
- AlarmManager
calls disableWifi()
in abc
to end WiFi AlarmManager
调用abc
disableWifi()
结束WiFi
END PROCEDURE 结束程序
You can create a separated class that depends on Context
. 您可以创建一个依赖于
Context
的单独的类。 Both Activity
and a BroadcastReceiver
can provide a Context
to this class. Activity
和BroadcastReceiver
都可Context
此类提供Context
。
Activity
is a subclass of Context
, so you can pass the Activity
as parameter. Activity
是Context
的子类,因此您可以将Activity
作为参数传递。 And the first parameter of onReceive
from BroadcastReceiver
is the Context
. 而来自
BroadcastReceiver
的onReceive
的第一个参数是Context
。
So you can create an class that receives an Context
instance an use the WifiManager
: 因此,您可以使用
WifiManager
创建一个接收Context
实例的WifiManager
:
// This can run outside UI Thread
public static void enableWifi(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
// This must run on UI Thread
public static void verifyWifi(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo conn = wifi.getConnectionInfo();
CharSequence wifi_on = "Connected to " + conn.getSSID();
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, wifi_on, duration).show();
}
You can use this class to detect when Wifi
is enabled or change it's state. 您可以使用此类来检测
Wifi
或更改其状态。
public class NetworkChangeReceiver extends BroadcastReceiver {
// Static Listener, you can change this
static OnNetworkConnectionChangedListener mListener;
// Service to check if Wifi is connected or not
ConnectivityManager mConnectivityManager;
@Override
public void onReceive(Context context, Intent intent) {
if(mListener != null) {
if(mConnectivityManager == null) {
mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
mListener.onConnectionChanged(NetworkStatusHelper.getConnectivityStatus(mConnectivityManager) != NetworkStatusHelper.TYPE_NOT_CONNECTED);
}
}
public static boolean isConnected(Context context) {
return NetworkStatusHelper.getConnectivityStatus((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)) != NetworkStatusHelper.TYPE_NOT_CONNECTED;
}
public static void setOnNetworkConnectionChangedListener(OnNetworkConnectionChangedListener listener) {
mListener = listener;
}
public interface OnNetworkConnectionChangedListener {
public void onConnectionChanged(boolean connected);
}
}
With this you i'll need to require the ACCESS_NETWORK_STATE
permission. 与此相关的是,我需要
ACCESS_NETWORK_STATE
权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
And you i'll need to add a receiver
for CONNECTIVITY_CHANGE
and WIFI_STATE_CHANGED
而且您需要为
CONNECTIVITY_CHANGE
和WIFI_STATE_CHANGED
添加一个receiver
<receiver
android:name="yourpackage.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
With this you can track when Wifi
change its connectivity and is connected. 有了它,您可以跟踪
Wifi
何时更改其连接性并已连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.