Android Widget 并点击 Listener

标签 android android-widget appwidgetprovider

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

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

我做错了什么?

@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 上测试

最佳答案

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

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

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

关于Android Widget 并点击 Listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396009/

相关文章:

android - 如何在 android jelly bean 上读取系统 logcat?

Android:将 WebView 输出加载到 App Widget

Android 权限拒绝 : broadcasting Intent

android - 以编程方式添加的 RadioButtons 拒绝服从 LayoutParams 加权

android - 使用 appWidgetId 检查主屏幕上是否存在小部件

java - Android Oreo 上小部件的屏幕开/关广播监听器

java - 如何使用 Activity 的 ListView 更新 App Widget

android - 在 Google map android 上放置点击监听器

android - 应用商店销售的30%佣金到底去了哪里?

android - Blackberry 和 Android 应用程序上的表情符号(表情符号)。如何支持?