为什么我的Android Clock小部件未显示在小部件列表中

时间:2019-03-09 21:15:16

标签: android widget clock

我尝试构建一个时钟小部件。
我在YouTube上找到了一个this视频教程,该视频教程是在 Eclipse 中创建窗口小部件的。
我在android studio中使用了代码。
这是我的代码:

  

xml /clock_widget.xml

<?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/clock_widget"
    android:minWidth="294dp"
    android:minHeight="146dp"
    android:previewImage="@mipmap/ic_launcher"
    android:updatePeriodMillis="0">
</appwidget-provider>
  

布局 /clock_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp">

    <RelativeLayout
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:background="@drawable/hour_border">

        <TextView
            android:id="@+id/hour"
            style="@style/hourWidgetFont"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:background="@drawable/hour_border">

        <TextView
            android:id="@+id/min"
            style="@style/hourWidgetFont"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</LinearLayout>
</RelativeLayout>
  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="srm.com.example">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".NewAppWidget"
        android:label="@string/clock_widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="srm.com.clockwidget.ClockWidget.CLOCK_WIDGET_UPDATE" />
        </intent-filter>

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

    <service android:name=".NewAppWidget$UpdateService" />
</application>

</manifest>
  

NewAppWidget.java

package srm.com.example;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;

import java.util.Calendar;
import java.util.Date;

public class NewAppWidget extends AppWidgetProvider {
public static final String CLOCK_WIDGET_UPDATE = "srm.com.clockwidget.ClockWidget.CLOCK_WIDGET_UPDATE";

private PendingIntent createUpdateIntent(Context context) {
    Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}

@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(createUpdateIntent(context));
}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 1);

    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000, createUpdateIntent(context));
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
        context.startService(new Intent(context, UpdateService.class));
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    context.startService(new Intent(context, UpdateService.class));
}

public static class UpdateService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        RemoteViews updateViews = buildUpdate(this);
        ComponentName widget = new ComponentName(this, NewAppWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(widget, updateViews);
    }

    private RemoteViews buildUpdate(Context context) {
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        updateViews.setTextViewText(R.id.hour, String.valueOf(c.get(Calendar.HOUR_OF_DAY)));
        int min = c.get(Calendar.MINUTE);
        updateViews.setTextViewText(R.id.min, (min < 10 ? "0" : "") + String.valueOf(min));
        return updateViews;
    }
}
}

1-没有错误,但我的窗口小部件未显示在窗口小部件列表中。
2-我要显示图像,而不是每分钟更改一次的时钟号。

0 个答案:

没有答案