我對android開發很陌生,我想爲我的應用程序創建一個小部件。不幸的是,我無法讓它工作。我希望我的小部件啓動一項服務。該服務應包含所有的邏輯,例如根據讀取內部存儲器文件的數據更新文本字段。由於我想顯示剩餘時間,因此我必須每分鐘更新一次小部件。爲此我想到了一個AlarmManager。Android小部件不叫服務onStart
我用這個作爲參考: http://www.helloandroid.com/tutorials/mastering-android-widget-development-part4
繼承人什麼我試過到目前爲止: 的的AppWidgetProvider:
public class WidgetCtrl extends AppWidgetProvider {
public static String WIDGET_UPDATE = "WIDGET_UPDATE";
public static int UPDATE_RATE = 60 * 1000;
private SessionData sessionData = SessionData.getInstance();
private BaseCtrl baseCtrl = BaseCtrl.getInstance();
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Service Intent
Intent serviceIntent = new Intent(context, UpdateWidgetService.class);
serviceIntent.setAction(UpdateWidgetService.UPDATE);
PendingIntent servicePendingIntent = PendingIntent.getService(context,
0, serviceIntent, 0);
// send Service intent
try {
servicePendingIntent.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// start alarm manager for all widget instances
for (int appWidgetId : appWidgetIds) {
setAlarm(context, appWidgetId, UPDATE_RATE);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
if (WIDGET_UPDATE.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
super.onReceive(context, intent);
}
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context, UpdateWidgetService.class));
super.onDisabled(context);
}
public static void setAlarm(Context context, int appWidgetId, int updateRate) {
Intent serviceIntent = new Intent(context, UpdateWidgetService.class);
serviceIntent.setAction(UpdateWidgetService.UPDATE);
PendingIntent servicePendingIntent = PendingIntent.getService(context,
0, serviceIntent, 0);
AlarmManager alarms = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
if (updateRate >= 0) {
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(), updateRate, servicePendingIntent);
} else {
// on a negative updateRate stop the refreshing
alarms.cancel(servicePendingIntent);
}
}
}
的服務:
public class UpdateWidgetService extends Service {
public static String UPDATE = "update";
private SessionData sessionData = SessionData.getInstance();
private BaseCtrl baseCtrl = BaseCtrl.getInstance();
private Context ctx;
@Override
public void onStart(Intent intent, int startId) {
ctx = getApplicationContext();
int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID);
// TODO update information and display
AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(ctx);
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName currWidget = new ComponentName(ctx, WidgetCtrl.class);
RemoteViews remoteViews = new RemoteViews(ctx.getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.widget_text, "test");
final Intent updateIntent = new Intent(ctx, UpdateWidgetService.class);
final PendingIntent pending = PendingIntent.getService(ctx, 0, updateIntent, 0);
final AlarmManager alarm = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 60;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
appWidgetManger.updateAppWidget(appWidgetId, remoteViews);
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我的問題是,它似乎是服務永遠不會被調用。我在服務的onStart方法中設置了斷點,但我從來沒有達到過。 有人可以幫忙嗎?
另請告訴我,如果你建議我的程序有不同的結構。
編輯: 我忘了添加清單代碼。在應用程序標籤之間,我將此代碼置於窗口小部件的接收器對象的後面:
<service android:name=".UpdateWidgetService"></service>
這是正確的嗎?
我真的很希望有人能給我一個解決方案或提示。
我我applicatin我onstartcommand刪除 http://stackoverflow.com/a/27674551/2782457 – nguyenvangiangbn