2

I have a widget that is simply a textview, that I am attempting to update every second using an AlarmManager. The Log.d("test", "hello1") shows up in my LogCat when I run the app, but the Log.d("test", "service") does not show up, so it appears the program is not even reaching my MyService class.

I would greatly appreciate any help.

Thank you.

Here is my AppWidgetProvider class:

public class NetworkSpeedWidget extends AppWidgetProvider {

private PendingIntent service = null; 

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    Log.d("test", "hello");
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    final Intent i = new Intent(context, MyService.class);
    if (service == null)
    {
        service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    }
    Log.d("test", "hello1");
    m.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),1000,service);

}

}

Here is my MyService class:

public class MyService extends Service{
@Override
public void onCreate()
{
    Log.d("test", "service");
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    buildUpdate();

    return super.onStartCommand(intent, flags, startId);
}

private void buildUpdate()
{


    MainActivity object = new MainActivity();
    String objectString = object.getMegaBitsPerSecondString();

    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    ComponentName thisWidget = new ComponentName(this, NetworkSpeedWidget.class);
    int[]ids = manager.getAppWidgetIds(thisWidget);
    final int N = ids.length;
    for (int i = 0; i < N; i++){
        int awID = ids[i];
        RemoteViews v = new RemoteViews(getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.widgetTextView,objectString);            
        manager.updateAppWidget(awID, v);
    }
}

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

1 回答 1

2

我所有的代码都是正确的,我只需要在我的清单中声明它。

我只是添加了以下行:

<service android:enabled="true" android:name=".MyService" />

谢谢你的帮助。

于 2013-07-10T04:51:30.527 回答