3

每个人。我正在使用“WidgetQuotes Sample”在 Android 手机(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);
    }
     }

更新小部件服务.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_info.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" />

主.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);
     }
   }
4

2 回答 2

0

尝试重新启动设备(模拟器)。或清除主屏幕的缓存。

我在 HTC (Android 2.3.7) 上遇到了类似的问题。通过清除 HTC Sense 缓存解决(HTC 替换标准 Android 主屏幕...)

于 2013-04-24T05:12:26.470 回答
0

该应用程序无法安装到 sdcard,进入设备的设置->管理应用程序->找到该应用程序,然后单击“移动到手机”按钮(假设它实际上在 sdcard 上)..

于 2015-02-02T03:05:47.937 回答