应用程序小部件无法连接到某些设备中的服务器
app widget cannot connect to server in some devices
我在使用某些设备时遇到特殊问题。
我的问题是:
我正在开发一个带有应用程序小部件的应用程序,该应用程序和应用程序小部件必须连接到服务器。
应用程序在设备上完美连接到服务器,但应用程序小部件在某些设备上无法连接到服务器。
应用小部件提供商class:
public class WidgetOne extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName componentName = new ComponentName(context, WidgetOne.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_weather);
Intent intent = new Intent(context, WidgetOne.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(!Prefe.builder().getBool("isFirstRun",true)){
showLoading(context, remoteViews);
new getResponseAsyncWidget1(context, remoteViews).execute();
}
remoteViews.setOnClickPendingIntent(R.id.root_refresh_widget, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.img_refresh_wedget_1, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
网络class:
public class Networking {
OkHttpClient okHttpClient;
public Networking() {
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.callTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
}
public String get(String url){
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return "error";
}
}
异步任务 class :
private class getResponseAsyncWidget1 extends AsyncTask<String, Integer, String> {
String url = "//url"
RemoteViews remoteViews;
Context context;
public getResponseAsyncWidget1(Context context, RemoteViews remoteViews) {
this.context = context;
this.remoteViews = remoteViews;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
Networking networking = new Networking();
String response = networking.get(url);
return response; // return "error" in some devices Although the internet is connected
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if (response.matches("error")) {
// do something
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetOne.class), remoteViews);
}
else {
// do something
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetOne.class), remoteViews);
}
}
}
我的问题是 response
在某些设备中 return "error"
尽管已连接互联网
当 phone 的电源模式开启时,系统不会在后台为 运行 提供必要的服务。
要解决这个问题,可以使用这个代码:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
欢迎使用 Whosebug
可能您的问题与设备 Android 版本有关,因为从 Android 9 开始,不允许以明文形式从应用程序通过 HTTP 的更新流量,请参见此处:
我在使用某些设备时遇到特殊问题。
我的问题是: 我正在开发一个带有应用程序小部件的应用程序,该应用程序和应用程序小部件必须连接到服务器。 应用程序在设备上完美连接到服务器,但应用程序小部件在某些设备上无法连接到服务器。
应用小部件提供商class:
public class WidgetOne extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName componentName = new ComponentName(context, WidgetOne.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_weather);
Intent intent = new Intent(context, WidgetOne.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(!Prefe.builder().getBool("isFirstRun",true)){
showLoading(context, remoteViews);
new getResponseAsyncWidget1(context, remoteViews).execute();
}
remoteViews.setOnClickPendingIntent(R.id.root_refresh_widget, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.img_refresh_wedget_1, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
网络class:
public class Networking {
OkHttpClient okHttpClient;
public Networking() {
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.callTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
}
public String get(String url){
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return "error";
}
}
异步任务 class :
private class getResponseAsyncWidget1 extends AsyncTask<String, Integer, String> {
String url = "//url"
RemoteViews remoteViews;
Context context;
public getResponseAsyncWidget1(Context context, RemoteViews remoteViews) {
this.context = context;
this.remoteViews = remoteViews;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
Networking networking = new Networking();
String response = networking.get(url);
return response; // return "error" in some devices Although the internet is connected
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if (response.matches("error")) {
// do something
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetOne.class), remoteViews);
}
else {
// do something
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetOne.class), remoteViews);
}
}
}
我的问题是 response
在某些设备中 return "error"
尽管已连接互联网
当 phone 的电源模式开启时,系统不会在后台为 运行 提供必要的服务。 要解决这个问题,可以使用这个代码:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
欢迎使用 Whosebug
可能您的问题与设备 Android 版本有关,因为从 Android 9 开始,不允许以明文形式从应用程序通过 HTTP 的更新流量,请参见此处: