View on GitHub

reading-notes

Intent Filters

What is the purpose of using intent-filter?

Allow other apps to start an another activity.

How intent-filter works?

Criteria of the Intent object:

Example:

 <activity android:name="ShareActivity">
   <intent-filter>
      <action android:name="android.intent.action.SEND"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <data android:mimeType="text/plain"/>
      <data android:mimeType="image/*"/>
   </intent-filter>
</activity>
Handle the Intent.
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  // Get the intent that started this activity
  Intent intent = getIntent();
  Uri data = intent.getData();

  // Figure out what to do based on the intent type
  if (intent.getType().indexOf("image/") != -1) {
      // Handle intents with image data ...
  } else if (intent.getType().equals("text/plain")) {
      // Handle intents with text ...
  }
}

// Return a Result
// Create intent to deliver some kind of result data
 Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri"));
 setResult(Activity.RESULT_OK, result);
 finish();