我的小部件有问题,我使用的是note 8(带有nova启动器),并且每次系统要杀死我的小部件时,因为它说我的程序多次停止运行或无法回答,并且是电池浪费^ _ ^
窗口小部件只是一个简单的视图,它显示了我从另一个页面加载的文本(最后一个代码),并且应该显示给我前三行,当我单击窗口小部件时,它显示了另外三行(并且如果再次单击,它将再次下载页面并重新启动周期),这就是它的作用,但是,如果我重新启动电话,则小部件将停止工作,并且我将无法对其进行任何操作(如果我删除了小部件并再次添加到屏幕的首页仍被阻止),几个小时后又重新开始
几乎处于“ hello world”的水平,但是我不明白什么是行不通的
有任何提示吗?
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.trigun.widget" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- <activity
android:name="it.trigun.widget.MainActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
<receiver
android:name="it.trigun.widget.widget"
android:exported="true"
android:grantUriPermissions="true" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
</application>
</manifest>
小工具类(唯一的1个)
package it.trigun.widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Implementation of App Widget functionality.
*/
public class widget extends AppWidgetProvider {
private PendingIntent service = null;
private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (SYNC_CLICKED.equals(intent.getAction())) {
SharedPreferences prefs = context.getSharedPreferences("click", context.MODE_PRIVATE);
boolean clicked = prefs.getBoolean("click", false);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
watchWidget = new ComponentName(context, widget.class);
if (clicked) {
LoadData l = new LoadData();
l.appWidgetManager = appWidgetManager;
l.remoteViews = remoteViews;
l.watchWidget = watchWidget;
l.execute();
} else {
remoteViews.setViewVisibility(R.id.textViewL1, View.GONE);
remoteViews.setViewVisibility(R.id.textViewL2, View.VISIBLE);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
SharedPreferences.Editor editor = context.getSharedPreferences("click", context.MODE_PRIVATE).edit();
editor.putBoolean("click", !clicked);
editor.commit();
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
SharedPreferences.Editor editor = context.getSharedPreferences("click", context.MODE_PRIVATE).edit();
editor.putBoolean("click", false);
editor.commit();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
remoteViews.setOnClickPendingIntent(R.id.tutto, getPendingSelfIntent(context, SYNC_CLICKED));
String lastUpdated2 = savePage("http://mysite/url.php");
String[] testo = lastUpdated2.split("\n\n");
if (lastUpdated2.length() > 2) {
remoteViews.setTextViewText(R.id.textViewL1, testo[0]);
remoteViews.setTextViewText(R.id.textViewL2, testo[1]);
remoteViews.setViewVisibility(R.id.textViewL1, View.VISIBLE);
remoteViews.setViewVisibility(R.id.textViewL2, View.GONE);
}
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
editor = context.getSharedPreferences("click", context.MODE_PRIVATE).edit();
editor.putBoolean("click", false);
editor.commit();
}
public static String savePage(String urlString) {
String line, res = "";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
/*
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet(URL); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
*/
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
while ((line = reader.readLine()) != null)
res += line + "\n";
if (res.length() > 3)
res = res.substring(0, res.length() - 1);
Log.v("widget", res);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
class LoadData extends AsyncTask<Void, Void, Void> {
RemoteViews remoteViews;
ComponentName watchWidget;
AppWidgetManager appWidgetManager;
protected void onPostExecute() {
// TODO: check this.exception
// TODO: do something with the feed
}
@Override
protected Void doInBackground(Void... params) {
try {
String lastUpdated2 = savePage("http://mysite/url.php");
String[] testo = lastUpdated2.split("\n\n");
if (lastUpdated2.length() > 2) {
remoteViews.setTextViewText(R.id.textViewL1, testo[0]);
remoteViews.setTextViewText(R.id.textViewL2, testo[1]);
remoteViews.setViewVisibility(R.id.textViewL1, View.VISIBLE);
remoteViews.setViewVisibility(R.id.textViewL2, View.GONE);
}
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
小部件的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tutto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight=".49"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textViewL1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:textColor="#000000"
android:text="Loading" />
<TextView
android:id="@+id/textViewL2"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:textColor="#FF0000"
android:text="Loading" />
</LinearLayout>
</LinearLayout>
最后是下载页面的示例
2.000
200
13% 13%
07/04
10.000
990