3

我正在尝试使用启动时钟应用程序制作一个小部件。

为什么此代码在真实设备上不起作用?

我做错了什么?

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

}

我在模拟器上运行应用程序并单击小部件 - 这项工作。在安卓 4.0.3、2.2 上测试

4

1 回答 1

0

可能,您有一个新设备,它具有新的时钟实现,它不在 clockImpls 列表中。例如,如果您有新的 Xperia Z 时钟,您可以添加到列表中:

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

在某处创建完整列表的好主意。

于 2013-11-25T10:12:46.877 回答