Programming from 51 years old [copying sutras] Intent IntentFilter-reference

Intent

https://developer.android.com/guide/components/intents-common?hl=ja https://developer.android.com/guide/components/intents-filters.html?hl=ja#Receiving

Intent is a messaging object used when requesting an action from another app component.

Intent can either specify which component to start or let the Android system decide which component to start. The former is called an explicit Intent and the latter is called an implicit Intent. In the method of entrusting the component to be started to the Android system, the information (\ ) is described in the manifest of the application having the component to be searched so that the Android system can search the component.

By setting a simple action and necessary data in the implicit Intent object, you can start the ability to execute the action in another app. You do not need to specify the component to execute.

If you call startActivity () or startActivityResult () </ font> and pass an implicit Intent, the system will use the Intent filter to determine which apps are suitable for using that Intent. to start. When there are multiple apps that use Intent, a dialog is displayed so that the user can select the app.


In the following, we will explain the implicit Intent that executes general actions according to the type of application that can handle Intent.

COUTION
If your device doesn't have an app that can use implicit Intent, using startActivity () will cause the app to crash. To avoid the crash, call the resolveActibity () method (method of the Intent object) to check the existence of the application that can receive the Intent. If the result is non-null, there is an app that can receive Intent and you can safely call startActivity (). If the result is null, don't use that Intent as it will cause a crash.

Intent and IntentFilter

https://developer.android.com/guide/components/intents-filters.html?hl=ja

Intent facilitates communication between components. The following three are basic usage examples.

Intent usage example

- Start activity </ strong>
To start a new instance of Activity (create a new screen), pass Intent to startActivity () </ font> .. The activity to start is described in Intent, and necessary data is set.
If you want to receive the result when the activity is completed, use startActivityForResult () </ font>. The activity receives the result as an Intent object using onActivityResult () </ font> callback. - Service start </ strong>
Service is a component for background operation without UI. With Android5.0 (API21) or later, you can start the service with JobScheduler </ font>. In Android5.0 or earlier, start the service with the method of Service class. For one-time operations such as file download, pass Intent to startService (). Set the service and data to start in Intent.
If you have set the client server interface to the service, you can pass the Intent to bindService () </ font> and bind it to the service of other components. - Broadcast delivery </ strong>
Broadcast is a message that can be received by any app. The system delivers broadcasts about various system events such as terminal startup and charging start. To broadcast to other apps, pass the Intent to sendBroadcast () </ font> or sendOrderedBroadcast () </ font>.

Intent type | Explicit Intent Implicit Intent

- Explicit Intent </ strong>
Specify the package name of the target application or the full decoration class name of the component, and specify the application that executes Intent. In many cases, we use explicit Intent because we know the class name of the activity or service that the developer wants to start. - Implicit Intent </ strong>
Do not appoint a specific component. By setting the processing and necessary data in the Intent, it can be executed by the component of another application.
For example, if you want to show a certain place on the map to the user, request using Intent to display the place specified by another application on the map.

Use an implicit Intent when entrusting the component that handles the activity to the Android system, and an explicit Intent when the component is specific (not entrusted to the system).

\ </ strong>

1. Activity (A) creates an Intent that describes the action and passes it to startActivity () (startActivity (intent)) 2. Android system searches all apps and finds Intent filters that match Intent 3. If a matching Intent filter is found, the system calls the app's onCreate () method and passes the Intent 4. The searched app starts an Activity (B) that matches the Intent.

With implicit Intent, the Android system compares the content of the Intent with the Intent filter defined in the manifest file of other apps </ font>. When the Intent matches the Intent filter, the Android system starts its component to deliver the Intent object. If there are multiple matching apps, let the user select them in the dialog. The Intent filter specifies the type of intent the component wants to receive in the app's manifest file.

For example, you can declare an activity intent filter to start an activity directly from another app using a particular type of Intent. If you do not declare an Intent filter for the activity, you can only start the activity with an explicit Intent.

COUTION
Use explicit Intent when starting with Service. Do not use the service's Intent filter. Using an implicit Intent on a Service poses a security risk because it loses track of whether the service responds to the Intent and the user does not know the started service. In Android 5.0 and above, using bindService () with an implicit Intent causes the system to throw an exception.

Build intent

In the Intent object, set the information required to search for the component started by the Android system and the information required for the component to perform the action (below).

- Component name - Action - data - category - Extra - flag

Intent intent = new Intent(this,MainActivity.class);
intent.setComponent()     // setClass(),setClassName(),Intent Constructor
intent.setAction()        // Intent Constructor
intent.setData()          // setType setDataAndType
intent.CATEFORY_BROWSABLE // CATEGORY_LAUNCHER
intent.putExtar()
intent.setFlags()

// Android Developer intent :of public constructors(Context packageContext, Class<?> cls)reference
//Specified component(Class<?>)Create an Intent on
//Context is AndroidSystem<-> app :Pass Environment Information
■ Component name (ComponentName object)

- Starting component name </ strong>
Required for explicit intents. Also, when starting the service, use an explicit Intent to ensure security. --The ComponentName object can be specified using the target component's fully decorated class name (eg com.example.ExampleActivity). Specify with the following method etc. - setComponent() - setClass() - setClassName() - Intent constructor

■ Action

A string that specifies the general action (display or selection) performed.

--For broadcast intents, this string is an action that has already occurred and has been reported. Depending on the type of action (display or selection), the information that makes up other Intents, such as data and information contained in extras, is determined.

--In your app, the action to start is specified by Intent, or it is specified by Intent to call the component of your app from other apps, but Normally Intent Specifies action constants defined in the class and other framework classes </ font>.

- </ strong> - ACTION_VIEW </ font> </ strong>
When there is information that the activity displays to the user, such as the photo displayed in the app or the address displayed in the map app. Use the ACTION_VIEW </ font> constant for the intent and pass it to startActivity ().

- ACTION_SEND </ font> </ strong>
ACTION_SEND is shared Intent </ font>. If there is data that can be shared with another app such as a mail app or social sharing app, use the ACTION_SEND </ font> constant for Intent and pass it to startActivity ().

The constants that define other actions are> https://developer.android.com/reference/android/content/Intent.html?hl=ja (see Contents)

Other actions are defined elsewhere in the Android framework. For example, the action to open a specific screen of the system settings app is defined in Settings.

Intent actions are specified using setAction () or Intent constructor </ font>.

Below, after defining your own action (TIMETRAVEL), specify the package name of the application as a prefix (ACTION_TIMETRAVEL = " com.example.action.TIMETRAVEL ").

static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";

■ Data

A URI (Uri object) that refers to the data required to perform an action and the MINE type of the data. The type of data specified depends on the Intent's action. If the action is ACTION_EDIT </ font>, the data will be set to the document URI to edit.

When creating an Intent, it may be important to specify the data type (MIME) </ font> as well as the URI. Music cannot be played in the image display activity. Specify the MIME type of the data to allow the Android system to search for suitable components for the Intent.

However, it may be possible to infer MIME from the URI. When the data is content, the URI is on the device and is controlled by the ContentProvider, so the Android system can determine the MIME type from the URI.

<Data / MIME type settings>

--Set only data URI: setData () </ font> --Set MIME type only: setType () </ font> --Set both: Use setDataAndType ().

COUTION
When setting both URI and MIME type, do not use setData () and setType () (disable each other's values) There is a risk). When setting both, be sure to use setDataAndType ()
■ Category

A string that sets additional information about the type of component that handles the Intent. Most Intents don't require categories. You can set as many category descriptions as you like in Intent. Below are some examples of commonly used categories.

  • CATEFORY_BROWSABLE
    Depending on the target activity, the browser displays data such as images and email messages.
  • CATEGORY_LAUNCHER
    In the initial activity of the task, display the activity in the list of application launchers of the system.
■ Extra

By component name, action, data, or category, the Android system searches for the appropriate app component. Additional information that does not affect this search can be found in the Intent Extras </ font>.

Set the information required to execute the request action with a key / value pair. Actions use specific types of data URIs or specific extras.

Extra data is the putExtra () </ font> method, which adds key and value parameters. Bundle </ font> can set all extra data, and putExtras () can be used to set Bundle to Intent </ font>.

For example, when creating an Intent to send an email using ACTION_SEND </ font>, use the EXTRA_EMAIL </ font> key to the recipient Or you can specify the subject using the EXTRA_SUBJECT </ font> key.

The Intent class can specify multiple EXTRA_ * </ strong> constants for standardized data types. If you want to declare your own extra key for the Intent your app receives, specify the app's package name as a prefix (below).

static final String EXTRA_GIGAWATTS = "com.example.EXTRA_FIFAWATTS";
COUTION:
Do not use Parcelable or Serializable when sending Intents received by other apps. If the app tries to access the Bundle object data and cannot access the parceling or serialization classes, the system will throw a RuntimeException.
■ Flag

Defined in the flag Intent class. Flags function as Intent metadata (information (name, date, extension etc.) that indicates the data itself, such as files and folders). The flag specifies to the Android system how to start the activity (what task the activity belongs to, etc.) and how to handle it after starting (whether to include it in the recent activity list, etc.).

□ setFlags --Set the handling of Intent with the flag. The settings depend on the type of component executed by Intent. The ** FLAG_ACTIVITY_ * flag ** is used in ** ConText # startActivity **, and the ** FLAG_RECEIVER_ * flag ** is used in ** Context # sendBroadcast (Intent) **. (For example, if you set FLAG_ACTIVITY_NO_HISTORY, new activities will not be recorded in the stack history)

--setFlags () For more information> https://developer.android.com/reference/android/content/Intent.html?hl=ja#setFlags (int)

Explicit Intent, Implicit Intent Example

Explicit Intent example

Use explicit Intent when launching special app components. To create an explicit Intent, define the component name in the Intent object. Other Intent properties are optional.

<Example: Start Download Service to download a file from the web>

//Suppose you run it in an activity.'this'Is the context.
//fileUrl is the URL of the string.("http:://www.example.com/image.png ")
Intent downloadIntent = new Intent(this,DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

The Intent (Context, Class) constructor provides the Context to the app and the Class object to the component. In this way, this Intent explicitly initiates the DownloadService class to your app.

Implicit Intent example

Implicit Intent launches an app on a device that can specify an action and execute it. Implicit Intent is a good choice if you don't want to perform that action in your app, but you can do it in another app and you want the user to choose whether to use that app.

For example, if you have content that you want users to share with others, use the ACTION_SEND </ font> action to create an Intent and add an extra to specify the content to share. To do. Using that Intent to call startActivity () </ font> allows the user to choose which app to share the content with.

COUTION:
There may be no app that can handle the implicit Intent sent to startActivity (), or it may be inaccessible due to restrictions or settings on the file. In this case, the app will crash. To avoid a crash, there are activities that use resolveActivity () on the Intent object and use Intent if the result is non-null. If null, disable the process of issuing Intent and avoid crash. The code below is an example. In this example, the contents of the extra are specified by declaring the data type of Intent without using URI.

python


//Generate a text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,textMessage);
sendIntent.setType("text/plain");

//Check if there is any activity that uses Intent
if(sendIntent.resolbeActivity(getPackageManager()) !=null){
   startActivity(sendIntent);
}

When startActivity () is called, the system looks at all installed apps and determines which apps can handle this type of Intent. The Intent must have the ACTION_SEND </ font> action and "text / plain" data set. If there are multiple such activities, let the user select the activity to start in the dialog (Tuser dialog).

App chooser display

When multiple apps respond to implicit intents, the user selects the app and makes it the default choice for action. Setting default choices is useful if the user selects the same app every time.

However, if there are multiple apps that respond to the Intent and the user may use a different app each time, explicitly display the chooser dialog (the default app for the action cannot be selected).

When an app performs a "share" with the ACTION_SEND </ font> action, the user may want to share using another app depending on the situation at the time. Always use a chooser dialog like the one below. alt

Chooser display code

Create an Intent using createChooser () </ font> and pass it to startActivity () </ font>. The following code will display in a dialog a list of apps that respond to the Intent passed to the createChooser () </ font> method. Use the specified text for the dialog title.

python


Intent sendIntent = new Intent(Intent.ACTION_SEND);
...

// "Use this ●● app"With that feeling
//Use string resources for UI text
String title = getResources().getString(R.string.chooser_title);

//Generate an Intent that displays the chooser dialog
Intent chooser = Intent.createChooser(sendIntent,title);

//Implicit Intent requires the presence of a responding action
//Check if there is an action to respond
if(sendIntent.sesolveActivity(getPackageManager())!=null){
   startActivity(chooser);
}

Receive an implicit Intent

To announce the implicit Intent that your app can receive, use the </ font> element in the manifest file to filter one or more Intent filters for each app component. Declare. Each Intent filter specifies the type of Intent it accepts, depending on the Intent's actions, data, and categories. The system delivers an implicit Intent to its app component only if the Intent passes one of the Intent filters.

Explicit Intent is always delivered to the target component, regardless of the Intent filter declared by the component.

The app component declares a filter individually for each unique job to be executed. For example, if one activity of the image gallery app has two filters, one to display the image and the other to edit, when you start this activity, it will return to the information in the Intent and display or edit the image. Decide which action to choose.

Each Intent filter is defined in the </ font> element of the app's manifest file and corresponds to the app component ( < Nest in activity> </ font> elements, etc.). In </ font>, use one or more of the following three elements and specify the Intent type to accept.

- \ </ font> </ strong>
In the name attribute, declare the action of the Intent to accept. The value should be the literal string of the action ("use this ●● action").

- \ </ font> </ strong>
Data URI (scheme / host / port / path) and one or more attributes of various MIME types Declare the data type to accept using.

- \ </ font> </ strong>
In the name attribute, declare the Intent category to accept. The value is a literal string of actions.

To receive an implicit Intent, include the CATEGORY_DEFAULT </ strong> category in the Intent filter. The
startActivity () </ strong> and startActivityForResult () </ strong> methods process all Intents as if they were declaring a CATEGORY_DEFAULT </ strong> category. To do. If the Intent filter does not declare a CATEGORY_DEFAULT </ strong> category, it will determine that there is no activity responding to the implicit Intent </ font> </ table>

The activity declaration of the Intent filter that receives ACTION_SEND </ font> Intent when the data type is text is illustrated below.

manifest.java


<activity ancroid:name="ShareActivity">
  <intent-filter>
    <action   ancroid:name     = "android.intent.action.SEND"/>
    <category android:name     = "android.intent.category.DEFAULT"/>
    <data     android:mimeType = "text/plain"/>
  </intent-filter>
</activity>

You can create a filter that contains two or more of \ \ \ </ font>. At that time, you need to make sure that the component can handle any combination of those filter elements </ font>.

If you want to support multiple types of Intent and only handle a specific combination of action data catetory </ strong> types, you need to create multiple Intent filters.

Implicit Intent is determined to fit each of these three elements and Intent against a filter. When the Intent passes the conformity judgment of all three elements, the Intent is delivered to the component. If even one does not match, the Android system will not deliver the Intent. However, if multiple Intent filters are set for one component, one of the filters may be passed.

COUTION: Intent filters can be dangerous to prevent other apps from using your app's components. The Intent filter limits a component to respond only to certain types of implicit Intents, but if another app specifies a component for your app and uses an explicit Intent, the component can start. There is sex. To prevent it from being used by another app, set the component's exported attribute to "false" instead of using the manifest Intent filter < / strong>.
Always start your service with an explicit Intent so you don't accidentally run the Service of another app.
Declare an Intent filter in the manifest file for all activities. However, broadcast receiver filters can be dynamically registered by calling registerReceiver () . Then use unregisterReceiver () to unregister the receiver. This allows the app to listen for a particular broadcast for a specific period of time while the app is running.
Filter example

The operation of the Intent filter will be explained using the manifest file of the social sharing application as an example.

python


<activity android:name="MainActivity">
    <!--Main activity to launch the app from the launcher-->
    <intent-filter>
        <action   android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="ShareActivity">
    <!--Activities that handle the process of sending text data-->
    <intent-filter>
        <action   android:name    ="android.intent.action.SEND"/>
        <category android:name    ="android.intent.category.DEFAULT"/>
        <data     android:mimeType="text/plain"/>
    </intent-filter>
    <!--Activity to send media data-->
    <intent-filter>
        <action   android:name="android.intent.action.SEND"/>
        <action   android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data     android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data     android:mimeType="image/*"/>
        <data     android:mimeType="video/*"/>
    </intent-filter>
</activity>

The first Intent filter is the main activity, the activity that opens the first time the user launches the app from the launcher icon.

- ACTION_MAIN </ font> </ strong> Action
The main entry point indicates that there is no Intent data. - CATEGORY_LAUNCHER </ font> </ strong> Category
Indicates that the icon for this activity should be placed in the system's app launcher. -If the \ </ font> element is an icon and no icon is specified, the system will use the \ </ font> element. Use the icon.

To see this activity in the app launcher, pair the two.

The second activity, ShareActivity, facilitates the sharing of text and media content. The user may enter this activity by navigating from MainActivity, but issues an implicit Intent that matches either of the two Intent filters (text data Intent filter or media data Intent filter). You can also enter ShareActivity directly from another app.

The MIME type of application / vnd.google.panorama360 + jpg is a special data type that specifies a panoramic photo and is processed by the Google Panorama API.

Use PendingIntent

The PendingIntent </ font> object is a wrapper for the Intent object. The main purpose is to give another app permission to use the contained Intent as if it was executed from the process of the app being developed.

Main use cases of PendingIntent

Pending means pending. It's like using Intent at some point instead of using it right away.

--Use notifications to declare the Intent to use when the user performs an action (Notification Manager on Android system executes the Intent).

--Use the app widget to declare the Intent that the user will use to perform the action (the app on the home screen will execute the Intent).

--Declare the Intent of the action to be executed at the specified time in the future (AlarmManager of Android system executes the Intent).

Since it is assumed that each Intent object is processed by a specific type of component of the application, PendingIntent is created with the same assumption. When PendingIntent is used, the application to be developed does not execute Intent by calling startActivity () </ font>. Instead, call the following generation method to declare the desired component type when creating the PendingIntent.

- Intent to start Activity </ font> </ strong>
PendingInten.getActivity () - Intent to start Service </ font> </ strong>
PendingIntent.getService () - Start BroadcastReceiver Intent </ font> </ strong>
PendingIntent.getBroadcast ()

Unless the application to be developed receives PendingIntent from another application, the PendingIntent methods required when creating a PendingIntent are almost the above three (there are also getActivities (Context, int, Intent [], int)).

Each method receives the current app's Context </ font>, the Intent you want to wrap, and one or more flags (whether the Intent can be used multiple times) that specifies how to use the Intent.

PendingIntent developers A description of the Intent and actions performed by PendingIntent. Create an instance of PendingIntent class by the following method.

- getActivity(Context,int,Intent,int) - getActivities(Context,int,Intent,int) - getBroadcast(Context,int,Intent,int) - getService(Context,int,Intent,int)

Argument description </ strong> getActivity (Activity starting with PendingInten, PendingIntent Source private code, Executing intent, Flag)

The created PendingIntent instance functions to run other applications after the fact. When PendingIntent is passed to another app, the passed app is given the same execution permission as the distribution source app itself (it is determined that it has the same permission and ID). Therefore, you need to be careful about how to generate PendingIntent. For example, when issuing a basic Intent, explicitly assign a component name so that the Intent can be delivered to the issue destination component without mistake. (Do not give important privileges to the wrong delivery destination)

For more information on using PendingIntent, see the usage examples below.

https://developer.android.com/guide/topics/ui/notifiers/notifications.html?hl=ja https://developer.android.com/guide/topics/appwidgets/index.html?hl=ja

Implicit Inten response judgment

When the system receives an implicit Intent that initiates an activity, it compares the Intent with the Intent filter to find the best activity for the Intent.

  • Action --data (both URI and data type)
  • category

Implicit Intent Action test

To specify which Intent actions to accept, declare zero or more \ </ font> elements in the Intent filter.

python


<intent-filter>
  <action android:name="android.intent.action.EDIT"/>
  <action android:name="android.intent.action.VIEW"/>
  ...
</intent-filter>

To pass this filter, the action specified by Intent must match the any </ font> action specified by \ .

If there are no actions in the list of filters (\ ~ </ intent-filter>), then there is no match for the Intent and all Intents cannot pass the test. However, if the Intent does not specify an action and the filter has one or more actions, the test will pass.

Implicit Intent Category test

When specifying a category, declare 0 or more \ </ font> elements in the Intent filter.

python


<intent-filter>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  ...
</intent-filter>

All categories of Intent must match the categories in the filter for the Intent to pass the category test. If there are more categories specified by the Intent filter than the categories specified by Intent, Intent may pass. For example, an intent with no category will always pass the test, regardless of the category declared in the filter.

Automatically CATEGORY_DEFAULLT category for all implicit Intents passed to startActivity () and startActivityForResult () Adapt to. If you want your activity to receive an implicit Intent, put the "android.intent.category.DEFAULT" category in \ Must be included .

Implicit Intent Data Test

If you want to specify the Intent data to accept, declare 0 or more \ </ font> elements in the Intent filter.

python


<intent-filter>
  <data android:mimeType="video/mpeg" android:scheme="http" .../>
  <data android:mimeType="audio/mpeg" android:scheme="http" .../>
  ///
</intent-filter>

Each element allows you to specify the URI structure and data type (MIME media type). The URI consists of four attributes, scheme host port path </ font>, as shown below.

<scheme>://<host>:<port>/<path>

The following example describes possible values for these attributes.

content://com.example.project:200/folder/subfolder/etc

In this URI, the scheme is content, the host is com.example.project, the port is 200, and the path is folder / subfolder / etc.

Each of these attributes can be omitted in the \ </ font> element. However, there is a primary dependency.

--Ignore host if scheme is not specified --Ignore port if host is unspecified --Ignore path if both scheme and host are unspecified

When comparing the Intent's URI with the filter's URI specification, it compares only part of the URI contained in the filter.

--If only a scheme is specified in the filter, all filters with that scheme will match. --If the filter scheme and host are specified and no path is specified, all URIs with the same scheme and host will pass the filter. --If the filter specifies scheme, host, path, only URIs with the same scheme, host, path will pass the filter.

According to the path specification, it is possible to request only a partial match of the path name, including the wildcard asterisk (*).

The Data test compares both the Intent's URI and MIME type with the values specified in the filter. The rules or as follows.

--Intents with no URI or MIME type can pass the test only if the filter does not specify a URI or MIME type. --If the URI is specified and the MIME type is unspecified Intent (the type is not specified and cannot be inferred from the URI), the test is performed only when the URI matches the URI format of the filter and the filter does not specify the MIME type. Pass --If the URI is not specified and the MIME type is specified, the test passes only if the filter list has the same MIME type and the URI format is not specified. --For Intents with a specified URI and MIME type (explicit, inferred from the URI), pass the MIME type test only if the MIME type matches the type in the filter list.
The URI test passes the test if it matches the filter URI or if the Intent has a content: URI or file: URI and the filter does not specify a URI.
That is, if the filter lists only MIME types, we presume that the component supports content: data and file: data.

If the Intent specifies a URI or MIME type, the data test will fail if there is no element in \ td>

The last rule is a rule that assumes that a component can get local data from a file or content provider. The filter only lists the data types, and there is no need to explicitly specify the content: scheme or file: scheme. The following example is typical code showing that the element can get the image data from the content provider and display it on Androido.

python


<intent-filter>
  <data android:mimeType="image/*"/>
  ...
</intent-filter>

Most of the available data is provided by content providers (sharing information, an interface for connecting data within one process to code running in another process), so specify the data type and URI. Often the method of not specifying is used (specifying the data type allows the content provider to identify the required data).

Another common configuration is a filter that describes the scheme and data type. For example, the following element defines Android to get video data (video / _ * "from the component network (scheme =" http ") and execute an action.

python


<intent-filter>
  <data android:scheme="http" android:mimeType="video/*"/>
  ...
</intent-filter>

Matching with Intent

Matching the Intent with the Intent filter has the function of searching for the target component to start and searching for information about some components on the terminal. For example, the home app finds all activities that have an Intent filter that specifies the ACTION_MAIN </ font> action and the VATEGORY_LUNCHER </ font> category. Then, enter the data in the app launcher (register the app with the component that can be started in the launcher). Matching is established only if the Intent action and category pass the filter test.

The app you are developing can also match Intents in the same way as the home app. PackageManager has a query ... () method and a resolve ... () method. The query ... () method provides all the components that can receive the Intent, and the resolve ... () method determines the best component to respond to the Intent. For example, queryIntentActivities () provides all activities that can execute the argument Intent, and queryIntentService () provides a list of similar services. Neither method starts the component, it just provides a list. Broadcast receivers also have queryBroadcastReceivers () as a similar method.

General Intent

Alarm lock

calendar

camera

Contact / contact book app

Email

File storage

Local action

map

Music / video

New notebook

phone

Search

Setting

Text message

Web browser

Intent verification with Android Debug bridge

Sample Code (dereferenced from below)

Constitution

Project Faile Setting
Project01 Manifest.java register activity SubActivity
<Intent-filter>The set
MainActivity.java Button listener
activity_main.xml Place two buttons
SubActivity.java
activity_sub.xml TextView "Project01 SubActivity"
Project02 Manifest.java activity Main Activity
<Intent-filter>The set
MainActivity.java
activity_main.xml TextView "Project02 MainActivity"

Project01

Manifest.java

Manifest.java


<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

//■■■ Add ↓ ■■■
<activity android:name=".SubActivity">
    <intent-filter>
        <action android:name="sub_activity"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
//■■■ Up to here ■■■

MainActivity layout (activity_main.xml)

activity_main.xml


<Button
   android:id="@+id/button01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button01"
   />
<Button
   android:id="@+id/button02"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button02"
   />

MainActivity.java

MainActivity.java


@Override 
protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   Button button01 = findViewById(R.id.button01);
   Button button02 = findViewById(R.id.button02);

   button01.setOnClickListener(new View.OnClickListener(){
      @Override 
      public void onClick(View v){
         Intent intent_sub = new Intent("sub_activity");
         startActivity(intent_sub);
      }
   });

   button02.setOnClickListener(new View.OnClickListener(){
      @Override 
      public void onClick(View v){
         Intent intent_pro02_main = new Intent("pro02_main_activity");
         startActivity(intent_pro02_main);
      }
   });
}

SubActivity layout

activity_sub.xml


<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="SubActivity in Project01"/>

SubActivity.java does nothing

Project02

Manifest.java

Manifest.java


<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    //■■■ Add ↓ ■■■
    <intent-filter>
        <action android:name="pro02_main_activity"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    //■■■ Up to here ■■■
</activity>

Main Activity layout

activity_main.xml


<TextView
   android:text="MainActivity in Project02"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

MainActivity.java does nothing

Recommended Posts