从php文件更新android(主屏幕)小部件
我想制作一个android小部件,它将从php文件更新。我找到了很多关于小部件的示例,以及很多关于如何从php向活动检索数据的示例,但我没有做到这一点。 我需要类似于这个例子()的东西,但是我需要从我的php文件中获取数字,而不是一个随机数 php文件,它输出一个我需要包含在我的小部件中的单个数字() 这是WidgetProvider类,但是当我试图在onUpdate()中调用AsyncTask时,它崩溃了从php文件更新android(主屏幕)小部件,php,android,widget,Php,Android,Widget,我想制作一个android小部件,它将从php文件更新。我找到了很多关于小部件的示例,以及很多关于如何从php向活动检索数据的示例,但我没有做到这一点。 我需要类似于这个例子()的东西,但是我需要从我的php文件中获取数字,而不是一个随机数 php文件,它输出一个我需要包含在我的小部件中的单个数字() 这是WidgetProvider类,但是当我试图在onUpdate()中调用AsyncTask时,它崩溃了 public class MyWidgetProvider extends AppWid
public class MyWidgetProvider extends AppWidgetProvider {
private TextView textView;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
//call AsyncTask
String serverURL = "http://www.angryboards.com/data/larnaca/test.php";
new async().execute(serverURL);
String txt = textView.getText().toString();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Set the text
remoteViews.setTextViewText(R.id.update, txt);
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.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);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
private class async extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
}
公共类MyWidgetProvider扩展了AppWidgetProvider{
私有文本视图文本视图;
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
//调用异步任务
字符串serverURL=”http://www.angryboards.com/data/larnaca/test.php";
新建async().execute(serverURL);
String txt=textView.getText().toString();
//获取所有ID
ComponentName thisWidget=新的ComponentName(上下文,
MyWidgetProvider.class);
int[]allWidgetIds=appWidgetManager.getAppWidgetIds(thisWidget);
for(int-widgetId:allWidgetIds){
RemoteViews RemoteViews=新的RemoteViews(context.getPackageName(),
R.layout.widget_布局);
//设置文本
setTextViewText(R.id.update,txt);
//注册onclick侦听器
Intent Intent=新的Intent(上下文,MyWidgetProvider.class);
setAction(AppWidgetManager.ACTION\u APPWIDGET\u UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_id,appWidgetIds);
PendingEvent PendingEvent=PendingEvent.getBroadcast(上下文,
0,意图,挂起内容。标志(更新当前);
setOnClickPendingEvent(R.id.update,PendingEvent);
updateAppWidgetManager.UpdateAppWidgetId(widgetId,RemoteView);
}
}
私有类异步扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串响应=”;
for(字符串url:url){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
试一试{
HttpResponse execute=client.execute(httpGet);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null){
响应+=s;
}
}捕获(例外e){
e、 printStackTrace();
}
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
setText(结果);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tasos.widgettest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.tasos.widgettest.MainActivity"
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:icon="@drawable/logo"
android:label="Example Widget"
android:name="MyWidgetProvider" >
<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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="3000" >
</appwidget-provider>
在布局中,我有一个文本视图(android:id=“@+id/update”)
谢谢完成!我做了一个包含AsyncTask的服务,并从appwigdetprovider调用该服务 我的UpdateWidgetService.java
public class UpdateWidgetService extends Service {
public String string;
public String inputLine;
public StringBuilder txt;
public BufferedReader in;
public URL url;
@Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.widget_layout);
Intent clickIntent = new Intent(this.getApplicationContext(),MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext() , 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//refresh tapping the screen
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
new DownloadTask().execute();
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
try {
url = new URL("http://www.angryboards.com/data/larnaca/latest_wind_value.php");
in = new BufferedReader(new InputStreamReader(url.openStream()));
inputLine = null;
txt = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
string = in.readLine();
txt.append(inputLine);
txt.append('\n');
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(getApplication());
//get widget ids for widgets created
ComponentName widget = new ComponentName(getApplication(), MyWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widget);
//update text
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.update, txt);
//refresh widget to show text
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
}
}
这个网站上有很多下载php文件回声的例子。那么,您的特殊问题是什么呢?我已经成功地使用(HttpClient HttpClient=new DefaultHttpClient();HttpPost HttpPost=new HttpPost(“)…工作正常,但在widgetprovider中不同,我不知道如何处理..谢谢删除php代码,因为它与此无关。请发布下载代码。在widget中几乎相同。好的,谢谢您的时间。在widgetprovider中,您可以使用
remoteViews.setTextViewText()
正如您使用的Vogella示例中所做的那样。因此,这很容易,而且您已经完成了。但是您在onCreate()中调用了postData()因此,您正在主线程上执行此操作。这是唯一可能的,因为您使用的是旧的Android版本。开始时,请将代码放入AsyncTask中,并在onPostExecute中设置textview。完成此操作后,只需几个步骤即可将其适配为小部件。
public class MyWidgetProvider extends AppWidgetProvider {
private static final String LOG = "com.tasos.widgettest";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(),
UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
}