Android note 9. Intent Exception Handling and common system calls

Source: Internet
Author: User

Android note 9. Intent Exception Handling and common system calls
Intent Exception Handling and common system calls Jiangdg_VIPhttp: // blog.csdn.net/u0126375011. Intent Exception HandlingWhen an Intent object starts a component or application, an exception may occur due to incorrect component settings or the application is not installed. In general, the application exits unexpectedly when an exception occurs. Because, in order not to affect the normal use of our developed applications, try .... catch Method to capture an exception or catch a specified exception, and handle the exception in the Exception Handling Module (such as the Toast prompt ). Try {

Intent intent = new Intent (); intent. setClassName ("com. tencent. mm", "com. tencent. mm. app. MMApplication"); startActivity (intent );
} Catch (Exception e) {Log. I ("exception tag", e); // do not use e. printStackTrace (); Android is generally not recommended.} For example, when the Intent object cannot find the specified Activity, the ActivityNotFoundException exception will occur. We can handle it like this: try {
Intent intent = new Intent (); intent. setClassName ("com. tencent. mm", "com. tencent. mm. app. MMApplication"); startActivity (intent );
} Catch (ActivityNotFoundException e) {Toast. makeText (PocketSphinxDemo. this, "starting the client. Please wait...", Toast. LENGTH_SHORT). show ();} Ii. Common system callsDue to the needs of the project, I collected and summarized some source code for starting the system program using intent. The source code can be used in 4.0 system tests. Let's share it with you.
(1) Call Toast. makeText (PocketSphinxDemo. this, "starting the dial. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent1 = new Intent (); // create an intent intent1.setAction (Intent. ACTION_DIAL); // sets the intent Action attribute intent1.setData (Uri. parse ("tel: //"); // sets the Date attribute startActivity of intent (intent1); // starts Activity (2) Open the browser try {Toast. makeText (PocketSphinxDemo. this, "starting the browser. Please wait... ", Toast. LENGTH_SHORT ). show (); Uri uri = Uri. parse ("http://www.baidu.com"); // convert string to uri object Intent intent2 = new Intent (Intent. ACTION_VIEW, uri); // create an intent2.addFlags (intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent2); // start Activity} catch (ActivityNotFoundException e) {Toast. makeText (PocketSphinxDemo. this, "An error occurred while starting 'browser! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 3) Open Map try {Toast. makeText (PocketSphinxDemo. this, "opening the map... please wait... ", Toast. LENGTH_SHORT ). show (); Uri uri = Uri. parse ("geo: 38.899533,-77.036476"); // convert the string to the uri object Intent intent3 = new Intent (); intent3.setAction (Intent. ACTION_VIEW); intent3.setData (uri); intent3.addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent3);} catch (ActivityNotFoundException E) {Toast. makeText (PocketSphinxDemo. this, "An error occurred while starting 'map! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 3) edit the SMS (call the SMS sending program) Toast. makeText (PocketSphinxDemo. this, "the text message is being opened. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent4 = new Intent (Intent. ACTION_VIEW); // create an intent intent4.setType ("vnd. android-dir/mms-sms "); startActivity (intent4); (4) view the contact Toast. makeText (PocketSphinxDemo. this, "starting the contact. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent5 = new I Ntent (Intent. ACTION_VIEW, ContactsContract. contacts. CONTENT_URI); startActivity (intent5); (5) Open the camera Toast. makeText (PocketSphinxDemo. this, "starting the camera. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent7 = new Intent (); intent7.setAction (MediaStore. INTENT_ACTION_STILL_IMAGE_CAMERA); // start the camera app startActivity (intent7); (6) Open the Toast library. makeText (PocketSphinxDemo. this, "opening the image library. Please wait... ", Toast. LENGTH_SHORT ). show (); In Tent intent8 = new Intent (); intent8.setType ("image/*"); intent8.setAction (Intent. ACTION_GET_CONTENT); startActivity (intent8); (7) Open the calculator Toast. makeText (PocketSphinxDemo. this, "the calculator is being started. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent11 = new Intent (); intent11.setClassName ("com. android. calculator2 "," com. android. calculator2.Calculator "); // call setClassName to specify which application to start startActivity (intent11); (8) Enable system settings Intent intentSet = new Intent (Settings. ACTION_SETTINGS); startActivity (intentSet); (9) Turn on the clock try {intentclock = new Intent (); intentclock. setClassName ("com. android. deskclock "," com. android. clock. define Clock "); startActivity (intentclock);} catch (ActivityNotFoundException e) {Toast. makeText (PocketSphinxDemo. this, "An error occurred while starting the 'clock! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 10) Open File Manager try {Intent intentFile = new Intent (); intentFile. setAction (Intent. ACTION_VIEW); intentFile. setType ("text/plain"); startActivity (intentFile);} catch (ActivityNotFoundException e) {Toast. makeText (PocketSphinxDemo. this, "An error occurred while starting 'file manager! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 11) Open QQtry {Toast. makeText (PocketSphinxDemo. this, "the QQ terminal tool is being opened. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent12 = new Intent (); intent12.setClassName ("com. tencent. mobileqq "," com. tencent. mobileqq. activity. splashActivity "); intent12.addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent12);} catch (ActivityNotFoundException e) {Toast. makeText (P OcketSphinxDemo. this, "An error occurred while starting 'qq! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 12) Open try {Toast. makeText (PocketSphinxDemo. this, "starting the client. Please wait... ", Toast. LENGTH_SHORT ). show (); Intent intent4 = new Intent (); intent4.setClassName ("com. tencent. mm "," com. tencent. mm. ui. launcherUI "); intent4.addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent4);} catch (ActivityNotFoundException e) {Toast. makeText (PocketSphinxDemo. this, "enable'' exception! \ N check whether the application is installed. ", Toast. LENGTH_SHORT ). show () ;}( 13) restart the phone String cmd = "su-c reboot"; try {Toast. makeText (PocketSphinxDemo. this, "Restarting the phone. Please wait... ", Toast. LENGTH_SHORT ). show (); runtime.getruntime(cmd.exe c (cmd);} catch (IOException e) {// TODO Auto-generated catch block new AlertDialog. builder (PocketSphinxDemo. this ). setTitle ("Error "). setMessage (e. getMessage ()). setPositiveButton ("OK", null ). show ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.