skip to Main Content

Everyone. I’m using “WidgetQuotes Sample” to display some quotes on homescreen for android phone (Android 2.3). But the widget doesn’t appear or show up. I don’t know why.

Take a look at these code:

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_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" />

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);
     }
   }

2

Answers


  1. Try restart device(emulator).
    or clear cache of Home Screen.

    I was similar problem on HTC (Android 2.3.7).
    Solved by clear cache of HTC Sense(standard Android Home Screen replacement by HTC…)

    Login or Signup to reply.
  2. The app can’t be installed to the sdcard, go into your device’s settings->manage applications->find the app then click the ‘move to phone’ button (assuming that it was actually on the sdcard)..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search