Intent


We all know that android there are two objects in intent, are explicit Intent and implicit Intent two kinds. Since explicit Intent code coupling is relatively high, and often proportional to upgrade the software to be iterative development, and therefore often considered it is implicit Intent .
Implicit Intent is more subtle, he did not clear that the activities to be started, but specifies a series of more abstract action and category other information, and then by the system to analyze intent intentions, and finally found a suitable activity to start.

[Note] It should be noted that when specifying an action, there Intentis at least one implicit for each category, that is<category android:name="android.intent.category.DEFAULT" />

1. ExplicitIntent

Intent intent = new Intent();
intent.setClass(this, Main2Activity.class);
startActivity(intent);

2. ImplicitIntent

Need to specify the intent filter of Main2Activity in the configuration file:

<intent-filter>
   <action android:name="com.example.myapplication.Main2Activity.Test"></action>
   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Then, use the specified actionintent to proceedstartActivity

Intent intent = new Intent("com.example.myapplication.Main2Activity.Test");
startActivity(intent);

3. Open the specified domain name address webpage

Use an implicit filter to call the phone's model browser, and then specify the specific domain name address to open, such as:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);

Intent.ACTION_VIEWIs androidbuilt into the systemAction

4. Make a call

First add permissions to the configuration file:

<uses-permission android:name="android.permission.CALL_PHONE"/>

Then, use Intent, namely:

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:15208149754"));
startActivity(intent);

However, androidxthe permissions of the current version need to be dynamically applied, that is, a judgment needs to be made, that is, change to:

if(checkReadPermission(Manifest.permission.CALL_PHONE)){
    
    
    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:15208149754"));
	startActivity(intent);
}

public boolean checkReadPermission(String string_permission) {
    
    
    boolean flag = false;
    if (ContextCompat.checkSelfPermission(this, string_permission) == PackageManager.PERMISSION_GRANTED) {
    
    //已有权限
        flag = true;
    } else {
    
    //申请权限
        ActivityCompat.requestPermissions(this, new String[]{
    
    string_permission}, 0);
    }
    return flag;
}

Then you can call the built-in program for calling, here isIntent.ACTION_CALL

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/113447795