时钟小工具秒针

时间:2013-11-25 05:51:38

标签: android android-widget clock

我正在用秒针制作一个时钟小部件。我从另一个网站获得了一个代码并已实现到我的project.i发现秒针效果很好但是在一段时间之后它变得很安静了.log cat已经发布了以下为您的参考

11-25 11:11:39.040: E/AndroidRuntime(23673): FATAL EXCEPTION: main
11-25 11:11:39.040: E/AndroidRuntime(23673): java.lang.RuntimeException: Unable to start service com.themebowl.christmaswinterclockfree.WidgetService@41ded6e8 with null: java.lang.NullPointerException
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2571)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.app.ActivityThread.access$2000(ActivityThread.java:140)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.os.Looper.loop(Looper.java:137)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at android.app.ActivityThread.main(ActivityThread.java:4921)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at dalvik.system.NativeStart.main(Native Method)
11-25 11:11:39.040: E/AndroidRuntime(23673): Caused by: java.lang.NullPointerException
11-25 11:11:39.040: E/AndroidRuntime(23673):    at com.themebowl.christmaswinterclockfree.ClockWidgetProvider.onDrawClock(ClockWidgetProvider.java:192)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at com.themebowl.christmaswinterclockfree.WidgetService.onStartCommand(WidgetService.java:64)
11-25 11:11:39.040: E/AndroidRuntime(23673):    at  android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2554)
11-25 11:11:39.040: E/AndroidRuntime(23673):    ... 10 more

我需要帮助。你会帮助我。

ClockWidgetProvider.class

package com.themebowl.christmaswinterclockfree;


import com.themebowl.christmaswinterclockfree.R.drawable;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.PowerManager;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews;

public class ClockWidgetProvider extends AppWidgetProvider {

public static String THEMEVALUE = "DEFAULT";
public static String SKINVALUE = "CYAN";

public static int DISPLAYWIDTH;
public static int DISPLAYHEIGHT;
private static Canvas dial_Canvas;
private static Drawable dial_drawable, hour_drawable, minute_drawable,
        second_drawable
        ;
private static boolean mChanged = false;
protected static Time mCalendar = new Time();
private static float mMinutes;
private static float mHour;
private static float mSecond;
private static RemoteViews views;
private static Bitmap b;
private static AppWidgetManager widgetManager;
private static int widgetId;

public static boolean hasBackground = false;
public static boolean hasNumbers = false;
public static boolean hasMarkers = false;
public static boolean isWidgetCredated = false;

private PowerManager.WakeLock wl;
private PowerManager pm;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    Log.e("onUpdate", "Inside onUpdate");

    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];
        updateAppWidget(context, appWidgetManager, appWidgetId);

    }

}



public static void updateAppWidget(Context context,
        AppWidgetManager appWidgetManager, int appWidgetId) {

    widgetManager = appWidgetManager;
    widgetId = appWidgetId;

    Intent intent = new Intent(context, ClockWidgetConfig.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); // Identifies
                                                                        // the
                                                                        // particular
                                                                        // widget...
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    PendingIntent pendIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

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


     if (THEMEVALUE.equalsIgnoreCase("NEON")) {


             //             second_drawable = context.getResources().getDrawable(
           //                       drawable.neon_blue_markers);

        dial_drawable = context.getResources().getDrawable(
                drawable.xclockb);
        hour_drawable = context.getResources().getDrawable(
                drawable.clock_hand_hour);
        minute_drawable = context.getResources().getDrawable(
                drawable.clock_hand_minute);

        second_drawable = context.getResources().getDrawable(
                drawable.xclocks);


    DISPLAYWIDTH = dial_drawable.getIntrinsicWidth();
    DISPLAYHEIGHT = dial_drawable.getIntrinsicHeight();

    b = Bitmap.createBitmap(DISPLAYWIDTH, DISPLAYHEIGHT,
            Bitmap.Config.ARGB_8888);
    dial_Canvas = new Canvas(b);
    onTimeChanged();

    views.setOnClickPendingIntent(R.id.Widget, pendIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);
     }
}

public void onReceive(Context context, Intent intent) {

    Log.e("onReceive", "Inside onReceive");

    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(intent)) {
        AppWidgetManager amgr = AppWidgetManager.getInstance(context);
        int length = amgr.getAppWidgetIds(intent.getComponent()).length;
        if (length == 0)// WidgetService.StopService(context);
            context.stopService(new Intent(context, WidgetService.class));
        isWidgetCredated = false;
        WidgetService.timer.cancel();
        WidgetService.timer.purge();
    }

    pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AnalogClock");
    //am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

//  setAlarm(context);

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

    Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(
            Intent.CATEGORY_LAUNCHER).setComponent(
            new ComponentName("com.android.alarmclock",
                    "com.android.alarmclock.AlarmClock"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            AlarmClockIntent, 0);
    views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

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

}

protected static void onDrawClock() {

    boolean changed = mChanged;
    if (changed) {
        mChanged = false;
    }

    Log.e("Inside onDraw", "Inside onDraw");

    int availableWidth = DISPLAYWIDTH;
    int availableHeight = DISPLAYHEIGHT;

    int x = availableWidth / 2;
    int y = availableHeight / 2;

    final Drawable dial = dial_drawable;
    int w = dial.getIntrinsicWidth();
    int h = dial.getIntrinsicHeight();

    boolean scaled = false;

    // //////////////////////



    // ////////////////////////////////
    if (availableWidth < w || availableHeight < h) {
        scaled = true;
        float scale = Math.min((float) availableWidth / (float) w,
                (float) availableHeight / (float) h);
        dial_Canvas.save();
        dial_Canvas.scale(scale, scale, x, y);
    }

    if (changed) {
        dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
    }
    dial.draw(dial_Canvas);

    dial_Canvas.save();
    dial_Canvas.rotate(mHour / 12.0f * 360.0f, x, y);

    final Drawable hourHand = hour_drawable;
    if (changed) {
        w = hourHand.getIntrinsicWidth();
        h = hourHand.getIntrinsicHeight();
        hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
                + (h / 2));
    }
    hourHand.draw(dial_Canvas);
    dial_Canvas.restore();

    dial_Canvas.save();
    dial_Canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);

    final Drawable minuteHand = minute_drawable;
    if (changed) {
        w = minuteHand.getIntrinsicWidth();
        h = minuteHand.getIntrinsicHeight();
        minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
                + (h / 2));
    }
    minuteHand.draw(dial_Canvas);
   //edited

    //dial_Canvas.restore();

    if (WidgetService.hasSecondsHand) {
        // /////////
        dial_Canvas.restore();

        dial_Canvas.save();
        // dial_Canvas.rotate(mSecond / 30.0f * 360.0f, x, y);

        dial_Canvas.rotate(mSecond / 60.0f * 60.0f * 360.0f, x, y);

        Log.e("second onTimeChanged", mSecond / 30.0f * 360.0f + "");

        final Drawable secondHand = second_drawable;
        if (changed) {
            w = secondHand.getIntrinsicWidth();
            h = secondHand.getIntrinsicHeight();
            secondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y
                    + (h / 2));
        }
        secondHand.draw(dial_Canvas);
        // ////////////
    }

    dial_Canvas.restore();
    //edited
    //dial_Canvas.save();

    if (scaled) {
        dial_Canvas.restore();
        //dial_Canvas.save();

    }

    isWidgetCredated = true;
}

protected static void onTimeChanged() {

    mCalendar.setToNow();
    int hour = mCalendar.hour;
    int minute = mCalendar.minute;
    int second = mCalendar.second;

    mSecond = second / 60.0f;
    mMinutes = minute + second / 60.0f;
    mHour = hour + mMinutes / 60.0f;
    mChanged = true;

    b.eraseColor(Color.TRANSPARENT);

    onDrawClock();

    views.setImageViewBitmap(R.id.dialimg, b);
     widgetManager.updateAppWidget(widgetId, views);

}

protected static void broadcastTimeChanging() {
    mCalendar.setToNow();
    int hour = mCalendar.hour;
    int minute = mCalendar.minute;
    int second = mCalendar.second;

    mSecond = second / 60.0f;
    mMinutes = minute + second / 60.0f;
    mHour = hour + mMinutes / 60.0f;
    mChanged = true;
}




   }

0 个答案:

没有答案