大家。我正在使用「WidgetQuotes示例」在Android手機(Android 2.3)的主屏幕上顯示一些引號。但小部件不會顯示或顯示。我不知道爲什麼。小工具未顯示在小工具列表中(Android 2.3)
看看這些代碼:
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeskraps.quotes"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".WidgetQuotes" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_quotes_info" />
</receiver>
<service android:name=".UpdateWidgetService"></service>
<activity
android:name="com.codeskraps.quotes.Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.codeskraps.quotes.ACTION_WIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
</manifest>
WidgetQuotes.Java
package com.codeskraps.quotes;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class WidgetQuotes extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// Build the intent to call the service
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
// To react to a click we have to use a pending intent as the
// onClickListener is
// excecuted by the homescreen application
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
// Finally update all widgets with the information about the click
// listener
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
UpdateWidgetService.Java
package com.codeskraps.quotes;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
public class UpdateWidgetService extends Service {
private static final String TAG = UpdateWidgetService.class.getSimpleName();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart started");
// Create some random data
Random random = new Random();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds.length > 0) {
for (int widgetId : appWidgetIds) {
List<String> qList = getQuotes();
int nextInt = random.nextInt(qList.size());
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.widget_textview, qList.get(nextInt));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
}
super.onStart(intent, startId);
}
public List<String> getQuotes(){
List<String> qList = new ArrayList<String>();
qList.add("When Life Gives You Questions, Google has Answers");
qList.add("1f u c4n r34d th1s u r34lly n33d t0 g37 l41d ");
qList.add("Microsoft: \"You've got questions. We've got dancing paperclips.\"");
qList.add("If at first you don't succeed; call it version 1.0 ");
qList.add("There are 10 types of people in the world: those who understand binary, and those who don't.");
qList.add("I'm not anti-social; I'm just not user friendly");
qList.add("The glass is neither half-full nor half-empty: it's twice as big as it needs to be.");
qList.add("I would love to change the world, but they won't give me the source code");
qList.add("A Life? Cool! Where can I download one of those?");
qList.add("Artificial Intelligence is no match for Natural Stupidity.");
qList.add("Windows has detected you do not have a keyboard. Press 'F9\" to continue.");
qList.add("In a world without fences and walls, who needs Gates and Windows?");
qList.add("MICROSOFT = Most Intelligent Customers Realize Our Software Only Fools Teenagers");
qList.add("\"Concept: On the keyboard of life, always keep one finger on the escape button.\"");
qList.add("My software never has bugs. It just develops random features.");
qList.add("The box said 'Requires Windows 95 or better'. So I installed LINUX.");
qList.add("Never make fun of the geeks, one day they will be your boss.");
qList.add("Girls are like internet domain names, the ones I like are already taken.");
qList.add("Better to be a geek than an idiot.");
qList.add("Failure is not an option -- it comes bundled with Windows.");
return qList;
}
}
widget_quotes_inf o.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="180000"
android:initialLayout="@layout/widget" />
Main.Java
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sendBroadcast(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME));
setContentView(R.layout.main);
}
}
它會給你任何錯誤嗎? – 2013-03-10 22:09:10