Android開発のApp Components

11989 ワード

Androidのアプリケーションフレームワークでは、再利用可能なコンポーネントのセットを豊富かつ革新的なアプリケーションで作成できます.この記事では、どのように構築されたコンポーネントがアプリケーションの構築ブロックを定義し、意図的にどのように接続するかを説明します.
  • Intents and Intent Filters
  • Activities
  • Services
  • Content Providers
  • App Widgets
  • Processes and Threads

  • Intent Typeは明示的意図(Explicit intents)と暗黙的意図(Implicit intents)に分けられ、下図に示す.1つの暗黙的な意図を記述することは、システムを介して別のアクティビティを開始することである:[1]アクティビティは、意図的なアクション記述を作成し、startActivity()に渡すことである.[2]のAndroidシステムは、すべてのアプリケーションの意図フィルタが一致する意図を検索します.マッチングが見つかった場合、[3]システムは、itsonCreate()メソッドを呼び出し、その意図を伝達することによって、マッチングのアクティビティ(アクティビティB)を開始する.警告:アプリケーションが安全であることを確認し、最初は常に明示的な意図的なサービスを使用し、サービスに意図的なフィルタを宣言しません.暗黙的な意図を使用してサービスを開始することは、サービス応答の目的と、ユーザーがどのサービスの開始を見ることができないため、セキュリティ上の危険性です.
    システムアルバムを呼び出してactionに転送するなど、システムアルバムを呼び出すことができます.もちろん、Actionをカスタマイズすることもできます.例えば、
    static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";

    Activityの起動がstartActivity ForResultを通過した場合、Action:CATEGORY_を宣言する必要があります意図的なフィルタリングに使用されるDEFAULT.
    <activity android:name="TestActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>

    1つのアプリケーションにはどのくらいのContextオブジェクトがありますか?この問題は実は簡単だ.Application、Activity、およびServiceは継承関係から、最終的な親クラスがContextであることから、各ActivityはContextであり、各ServiceはContextであり、ApplicationもContextであると考えることができます.各アプリケーションの自己起動にはアプリケーションが作成されます.アプリケーションにカスタムアプリケーションがある場合は、リストファイルmanifestに宣言が必要です.ActivityとServiceも必要です.
    開発中にシステムサービスを呼び出し、Actionフィルタリング、例えばシステムの目覚まし時計を呼び出すと、ACTION_SET_ALARMでいいです.インスタンスコードは次のとおりです.
    public void createAlarm(String message, int hour, int minutes) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    システム・サービスを呼び出すには、目覚まし時計の設定を例に挙げると、次のような機能要求に対応する権限が必要です.
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    次はいくつかの列のシステム機能呼び出しコードブロックです.まずカウントダウンを見てみましょう(Android 4.4)
    public void startTimer(String message, int seconds) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_LENGTH, seconds)
                .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    カレンダーにイベントを追加し、インスタンスと意図的にフィルタします.
    public void addEvent(String title, String location, Calendar begin, Calendar end) {
        Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(Events.CONTENT_URI)
                .putExtra(Events.TITLE, title)
                .putExtra(Events.EVENT_LOCATION, location)
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    <activity ...>
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
            <data android:mimeType="vnd.android.cursor.dir/event" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    呼び出しシステムCamera
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final Uri mLocationForPhotos;
    
    public void capturePhoto(String targetFilename) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.withAppendedPath(mLocationForPhotos, targetFilename);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bitmap thumbnail = data.getParcelable("data");
            // Do other work with full size photo saved in mLocationForPhotos
            ...
        }
    }

    連絡先の読み出し(READ_CONTATS)
    static final int REQUEST_SELECT_CONTACT = 1;
    
    public void selectContact() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_CONTACT);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            // Do something with the selected contact at contactUri
            ...
        }
    }

    連絡先情報の照会
    static final int REQUEST_SELECT_PHONE_NUMBER = 1;
    
    public void selectContact() {
        // Start an activity for the user to pick a phone number from contacts
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
            // Get the URI and query the content provider for the phone number
            Uri contactUri = data.getData();
            String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, projection,
                    null, null, null);
            // If the cursor returned is valid, get the phone number
            if (cursor != null && cursor.moveToFirst()) {
                int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(numberIndex);
                // Do something with the phone number
                ...
            }
        }
    }

    このような方法はブロックが多すぎると一つ一つ列挙されない.Androidシステムで設定した機能モジュールの開き方は、managerで実現するほか、intentで意図的に開くこともできます.コードは以下の通りです.
    public void openWifiSettings() {
        Intent intent = new Intent(Intent.ACTION_WIFI_SETTINGS);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    ACTION:ACTION_SETTINGS
           ACTION_WIRELESS_SETTINGS
           ACTION_AIRPLANE_MODE_SETTINGS
           ACTION_WIFI_SETTINGS
           ACTION_APN_SETTINGS
           ACTION_BLUETOOTH_SETTINGS
           ACTION_DATE_SETTINGS
           ACTION_LOCALE_SETTINGS
           ACTION_INPUT_METHOD_SETTINGS
           ACTION_DISPLAY_SETTINGS
           ACTION_SECURITY_SETTINGS
           ACTION_LOCATION_SOURCE_SETTINGS
           ACTION_INTERNAL_STORAGE_SETTINGS
           ACTION_MEMORY_CARD_SETTINGS