我正在研究一个不断脱离WiFi的android服装应用程序。有没有办法可以通过编程方式保持WiFi连接稳定?我知道更多的硬件问题,但我看到一些有助于保持WiFi连接稳定的应用程序。
答案 0 :(得分:0)
正如您所提到的,这是一个硬件问题。我建议的最好是运行你可以离线的任何东西,然后在连接可用时让在线功能赶上
答案 1 :(得分:0)
您可以使用broadcastreceiver检查互联网是否连接如下。
Manifest.Java
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- This permissions are neccessary for broadcast receiver -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:enabled="true" >
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
</application>
MyApplication.Java
公共类MyApplication扩展Application { //在整个应用程序中使用的Gloabl变量声明
public static boolean activityVisible; // Variable that will check the
// current activity state
public static boolean isActivityVisible() {
return activityVisible; // return true or false
}
public static void activityResumed() {
activityVisible = true;// this will set true when activity resumed
}
public static void activityPaused() {
activityVisible = false;// this will set false when activity paused
}
}
Receiver.Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
/**
* Created by root on 7/4/17.
*/
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
boolean isVisible = MyApplication.isActivityVisible();// Check if
// activity
// is
// visible
// or not
Log.i("ActivityVisible ", "visible : " + isVisible);
// If it is visible then trigger the task else do nothing
if (isVisible == true) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
// Check internet connection and accrding to state change the
// text of activity by calling method
if (networkInfo != null && networkInfo.isConnected()) {
new MainActivity().changeTextStatus(true);
Log.i("ActivityVisible ", "visible : " + isVisible);
} else {
new MainActivity().changeTextStatus(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
MainActivity.Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static TextView internetStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
internetStatus = (TextView) findViewById(R.id.internet_status);
// At activity startup we manually check the internet status and change
// the text status
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
changeTextStatus(true);
} else {
changeTextStatus(false);
}
}
// Method to change the text status
public void changeTextStatus(boolean isConnected) {
// Change status according to boolean value
if (isConnected) {
internetStatus.setText("Internet Connected.");
internetStatus.setTextColor(Color.parseColor("#00ff00"));
} else {
internetStatus.setText("Internet Disconnected.");
internetStatus.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();// On Pause notify the Application
}
@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();// On Resume notify the Application
}
}