Android应用程序模拟时钟小部件无效

时间:2015-11-01 00:27:12

标签: android android-intent widget android-widget clock

我正在尝试为应用创建模拟时钟小部件。我已经完成了,时钟工作,但我想要点击它打开默认或安装的时钟应用程序。

这是我实施的代码。几个月前它正在运作。 (因为大学,我不得不停止开发)。

有人可以告诉我如何解决它并让它再次起作用吗?

编辑:我确定代码会在views.setOnClickPendingIntent()部分中输入...但时钟应用程序尚未启动/打开。

提前致谢。

CODE:

public class ClockWidget extends AppWidgetProvider {

    public HashMap<String, String> activityMap;
    PackageManager packageManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        packageManager = context.getPackageManager();
        boolean foundApp = false;

        setupHashMap();
        String action = intent.getAction();

        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {

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

            Intent clockAppIntent = new Intent();
            clockAppIntent.setAction(Intent.ACTION_MAIN);
            clockAppIntent.addCategory(Intent.CATEGORY_LAUNCHER);

            for (String packageName : activityMap.keySet()) {
                if (Util.isAppInstalled(context, packageName)) {
                    ComponentName cn = new ComponentName(packageName, activityMap.get(packageName));
                    clockAppIntent.setComponent(cn);
                    clockAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    foundApp = true;
                    break;
                }
            }

            if (foundApp) {
                views.setOnClickPendingIntent(R.id.clockWidget,
                        PendingIntent.getActivity(context, 0, clockAppIntent, 0));
            }

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

        }
    }

    private void setupHashMap() {
        activityMap = new HashMap<>();
        activityMap.put("com.android.alarmclock", "com.android.alarmclock.AlarmClock");
        activityMap.put("com.android.deskclock", "com.android.deskclock.DeskClock");
        activityMap.put("com.google.android.deskclock", "com.google.android.deskclock.DeskClock");
        activityMap.put("com.google.android.deskclock", "com.android.deskclock.AlarmClock");
        activityMap.put("com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage");
        activityMap.put("com.sonyericsson.alarm", "com.sonyericsson.alarm.Alarm");
        activityMap.put("com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock");
        activityMap.put("com.asus.alarmclock", "com.asus.alarmclock.AlarmClock");
        activityMap.put("com.asus.deskclock", "com.asus.deskclock.DeskClock");
        activityMap.put("com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl");
        activityMap.put("com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock");
        activityMap.put("com.lge.clock", "com.lge.clock.AlarmClockActivity");
    }

}

清单:

<receiver
            android:name=".widgets.ClockWidget"
            android:label="@string/clock_widget_title">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/clock_widget_provider"/>
        </receiver>

布局:

<?xml version="1.0" encoding="utf-8"?>
<AnalogClock
    android:id="@+id/clockWidget"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:dial="@drawable/clock_bg"
    android:hand_hour="@drawable/clock_hour_hand"
    android:hand_minute="@drawable/clock_minute_hand"/>

PROVIDER:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/clock_widget"
    android:minHeight="144dip"
    android:minWidth="144dip"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen"/>

1 个答案:

答案 0 :(得分:0)

以下是一些代码片段,可帮助您开始重构那个巨大的if()块。

HashMap activityMap;    

private void setupHashMap() {
    // setup a map from the package name to the activity that
    // is responsible for the clock
    activityMap = new HashMap(String, String);
    activityMap.put("com.package.foo", "com.package.foo.AlarmClock");
    // ... add your items ...
}

private void launchAlarmClock() {
    // setup intent

    for (String packageName : activityMap.keySet()) {
        if (Util.appIsInstalled(context, packageName)) {
            Log.d(TAG, "Using the " + packageName + "alarm clock.");

            intent.setPackage(packageName);
            intent.setComponent(new ComponentName(packageName,
                        activityMap.get(packageName)));
            // do something with  the intent
        }
    }
}