2

私はAndroid開発にまったく慣れていないので、アプリケーション用のウィジェットを作成したいと思います。残念ながら、私はそれを機能させることができません。ウィジェットでサービスを開始したい。サービスには、内部ストレージのファイルのデータ読み取りに応じてテキストフィールドを更新するなどのすべてのロジックが含まれている必要があります。残り時間を表示したいので、ウィジェットを毎分更新する必要があります。このために私はAlarmManagerについて考えました。

私はこれを参照として使用しました: 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>

これは正しいです?

誰かが私に解決策やヒントを教えてくれることを本当に望んでいます。

4

2 に答える 2

3

OK、ついに問題が見つかりました:

マニフェストでは、サービスへのフルパスを追加する必要がありました。

そのため、.UpdateWidgetServiceをcom.example.UpdateWidgetServiceに変更する必要がありました。

これが他の人の検索の数時間を節約するのに役立つことを願っています;)

于 2012-12-12T14:14:32.820 に答える
1

マニフェストにサービスを追加するのを忘れました。私のappwidgetコードと数時間ジャグリングした後。最後に、私は私の小さな間違いを見つけました。次のマニフェストの追加は、リモートビューでデータを一覧表示するのに大いに役立ちました。

    <service
        android:name="np.com.bpb.capstoneroomtest2.widget.JobWidgetRemoteViewsService"
        android:exported="false"
        android:permission="android.permission.BIND_REMOTEVIEWS" >
    </service>
</application><!--Just before closing this tag, add your service-->

私の経験があなたのお役に立てば幸いです。

于 2018-10-19T20:20:40.577 に答える