1

(1) アクティビティから、IntentService の特定のメソッド (アクション) を呼び出すことはできますか? 以下のサンプル コードを使用して、ONLY ACTION_BAZ を呼び出したいと思います。

public class MyIntentService extends IntentService {

    private static final String ACTION_FOO = "com.example.application.action.FOO";
    private static final String ACTION_BAZ = "com.example.application.action.BAZ";

    private static final String EXTRA_PARAM1 = "com.example.application.extra.PARAM1";
    private static final String EXTRA_PARAM2 = "com.example.application.extra.PARAM2";

    public static void startActionFoo(Context context, String param1,
            String param2) {
        Intent intent = new Intent(context, MyIntentService.class);
        intent.setAction(ACTION_FOO);
        intent.putExtra(EXTRA_PARAM1, param1);
        intent.putExtra(EXTRA_PARAM2, param2);
        context.startService(intent);
    }

    public static void startActionBaz(Context context, String param1,
            String param2) {
            // pretty much identical
    }

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_FOO.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionFoo(param1, param2);
            } else if (ACTION_BAZ.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionBaz(param1, param2);
            }
        }
    }


    private void handleActionFoo(String param1, String param2) {
        // populates sqldatabase tables from asset .csv file
    }


    private void handleActionBaz(String param1, String param2) {
        // compliments onUpgrade database action by dropping tables in ActionFoo
    }
}

(2) 私の活動から、私は電話できる/すべきですか?

public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(SQL_CREATE_TABLE);
            // Starts MyIntentService queue for populating Table
            final Context svccontext = svccontext.getApplicationContext();
            final Intent intent = new Intent(svccontext, MyIntentService.class);
            intent.setAction(ACTION_FOO);
            svccontext.startService(intent);
            }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // the same as above, but calls method to drop tables
            intent.setAction(ACTION_BAZ);
        }

では、(1)のIntentServiceのメソッド(アクション)に引数param1、param2を指定してメソッドを呼び出していないのは問題でしょうか?

(3) 最後に、 Googleが提供するサンプル コード(deveoper.android.com)では、私の IntentService は常にメソッド (アクション) Foo と Baz の両方をこの順序で開始しますか? 私が完全にずれている場合は、私の理解を修正してください...


UI を遅らせないデータベース機能を実行するバックグラウンド サービスとして myIntentService を呼び出すことを意味します。私は持っているかもしれません

  • ActionFoo は、アセット .csv ファイルから MainActivity によって既に作成されたテーブルにデータを入力します。

  • MainActivity の onUpgrade 呼び出しの場合、ActionBaz は前述のテーブルをドロップする可能性があります。

  • バックグラウンドでアセットが入力されたテーブルを処理できる一方で、ユーザーはユーザー定義のテーブルに手動でデータを入力できました。

私の 3 番目の懸念は、myIntentService へのすべての呼び出しで、ActionFoo (テーブルにデータを入力) とそれに続く ActionBaz (作成したばかりのテーブルをドロップする) の両方が実行されるのでしょうか? または、私の onHandleIntent は、指定されたアクション (インテントに追加されたもの) のみが処理されることを確認しますか?

インテントの setAction とアクティビティ内のパラメータの両方を明示的に言及する必要がある場合、IntentService のヘルパー メソッド (つまり startActionFoo) はどのように役立ちますか? とにかく、それらはすべて呼び出しアクティビティに再定義する必要があります。

4

1 に答える 1

0

アクティビティから、IntentService の特定のメソッド (アクション) を呼び出すことはできますか?

アクションはメソッドとは直接関係ありません。Intent最初のサンプル コードで行っているように、明示的にアクション文字列を含めることができます。

私の活動から、電話してもよろしいですか?

いいえ、それは有効な Java ではないためです。startActionBaz()が返されるため、コンパイルされませんvoid

私の IntentService は常にメソッド (アクション) Foo と Baz の両方をこの順序で開始しますか?

ソースコードに「Foo」はありません。

于 2014-04-23T13:45:35.120 に答える