Android小部件和点击监听器

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

}

我在模拟器上运行应用程序并点击小部件-这个可以工作。已在Android 4.0.3、2.2上进行了测试。


你需要添加更多的日志语句并检查它在哪里被阻塞了。这个接收器类的意图过滤器是什么?还是一个AppWidgetProvider类? - San
1个回答

0

可能你有一款新设备,它有一个新的时钟实现,但这个实现不在时钟实现列表中。例如,如果你有新的Xperia Z时钟,你可以将其添加到列表中:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接