3

I'm trying to make a widget with the launch clock application.

Why this code doesn't work on real device ?

What i'm doing wrong ?

@Override
public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();
    PendingIntent pendingIntent;
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
    {

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.clock_appwidget);

        pendingIntent = PendingIntent.getActivity(context, 0,getAlarmPackage(context), 0);
        views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);

        AppWidgetManager.getInstance(context).updateAppWidget(
                        intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
                        views);
    }
}


public Intent getAlarmPackage(Context context)
{
    PackageManager packageManager = context.getPackageManager();
            Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);

    String clockImpls[][] = {
            { "Standard Alarm", "com.android.alarmclock",
                    "com.android.alarmclock.AlarmClock" },
            { "HTC Alarm ClockDT", "com.htc.android.worldclock",
                    "com.htc.android.worldclock.WorldClockTabControl" },
            { "Standard Alarm ClockDT", "com.android.deskclock",
                    "com.android.deskclock.AlarmClock" },
            { "Froyo Nexus Alarm ClockDT",
                    "com.google.android.deskclock",
                    "com.android.deskclock.DeskClock" },
            { "Moto Blur Alarm ClockDT",
                    "com.motorola.blur.alarmclock",
                    "com.motorola.blur.alarmclock.AlarmClock" },
            { "Samsung Galaxy S", "com.sec.android.app.clockpackage",
                    "com.sec.android.app.clockpackage.ClockPackage" } };

    boolean foundClockImpl = false;

    for (int i = 0; i < clockImpls.length; i++)
    {
        String packageName = clockImpls[i][1];
        String className = clockImpls[i][2];
        try
        {
        ComponentName cn = new ComponentName(packageName, className);
        packageManager.getActivityInfo(cn,PackageManager.GET_META_DATA);
            AlarmClockIntent.setComponent(cn);
            foundClockImpl = true;
        } catch (NameNotFoundException nf)
        {
            Log.v("foundclock", nf.toString());
        }
    }
    if (foundClockImpl)
    {
        return AlarmClockIntent;
    }
    else
    {
        return null;
    }

}

I run application on emulator and click on widget - this work. Tested on android 4.0.3, 2.2

mr.ANDREW
  • 51
  • 1
  • 7
  • You have to put more log statements and check where it is getting blocked. What are the intent filters for this receiver class? Or is it a AppWidgetProvider class? – San May 02 '12 at 14:29

1 Answers1

0

Probably, you have new device which has new clock implementation, which is not in the clockImpls list. For example, if you have new Xperia Z clock you can add to the list:

{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }   

Nice idea to create complete list somewhere.

Victor Laskin
  • 2,577
  • 2
  • 16
  • 10