The action properties of intent in the android tutorial use the example of intent for texting

  • 2020-05-24 06:09:24
  • OfStack

Action: specifies the action to be performed by Intent and is a string constant. Use setAction() to set the Action property, and getAction() to get the Action property. You can use either the built-in Action or you can define your own. System custom action, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, and so on.

1. Customize Action

Specify the action constant in AndroidManifest.xml for destination Activity.


<activity android:name=".ActionDestination">
   <intent-filter>
       <action android:name="Skywang_ACTION" />
       <category android:name="android.intent.category.DEFAULT"/>
   </intent-filter>
</activity>

< categoryandroid:name="android.intent.category.DEFAULT" / > Is used to show that you can find ActionDestination this activity by implicitly jumping (that is, other classes call setAction("Skywang_ACTION")). In this way, other classes can jump to ActionDestination with the following code. When jumping, the setAction string "Skywang_ACTION" must correspond to "Skywang_ACTION"1 as defined in AndroidManifest.xml.

Intent intent = new Intent();  
intent.setAction("Skywang_ACTION");  
startActivity(intent);

2 system Action


//  Traffic web page 
Uri uri =Uri.parse("http://www.baidu.com"); 
Intent intent = newIntent(Intent.ACTION_VIEW, uri);  
startActivity(intent); 
//  Make a phone call 
// if you want to use ACTION_DIAL, you mustadd permissin in manifest, the permission is bellow
// <uses-permissionandroid:name="android.permission.CALL_PHONE" />
Uri uri = Uri.parse("tel:12580"); 
Intent it = new Intent(Intent.ACTION_DIAL,uri); 
startActivity(it);
//  Send a text message 
Uri uri = Uri.parse("smsto:13410177756"); 
Intent it = newIntent(Intent.ACTION_SENDTO, uri); 
it.putExtra("sms_body", "TheSMS text"); 
startActivity(it);
// play mp3
Intent it = new Intent(Intent.ACTION_VIEW); 
Uri uri =Uri.parse("file:///sdcard/song.mp3");  
it.setDataAndType(uri, "audio/mp3"); 
startActivity(it);


Related articles: