Android Note Nine. Intent exception handling and common system calls

Source: Internet
Author: User

Intent exception handling and common system callsJIANGDG_VIPhttp://blog.csdn.net/u012637501 first, intent exception handlingWhen the intent object launches a component or an app, it can cause an exception because of a component's setup error or if the application is not installed. In general, an exception exits when an exception is applied. Because, in order not to affect the normal use of our developed application, you can catch the exception by Try....catch method or catch a specified exception, and handle the exception handling module (such as Toast hint). As shown below:try {
Intent intent=new Intent (); intent.setclassname ("com.tencent.mm", "com.tencent.mm.app.MMApplication"); startactivity (intent);
         }catch (Exception e) {LOG.I ("Exception label", e);//Do not use E.printstacktrace (); Android generally does not advocate the use of      }For example, when the intent object cannot find the specified activity, a activitynotfoundexception exception occurs , which we can do: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 client, please later ...", Toast.length_short). Show ();} second, common system callsdue to the needs of the project, I collected and summed up some of the use of intent startup System program source code, 4.0 system Pro-test can be used to share a common learning bar.
(1) Call Toast.maketext (Pocketsphinxdemo.this, "Starting dialer, please later ...", Toast.length_short). Show (); Intent intent1=new Intent (); Create a Intent intent1.setaction (intent.action_dial);//Set the ACTION property of the Intent Intent1.setdata (Uri.parse ("tel://"));// Set the date property of Intent startactivity (INTENT1);//Start activity//start activity (2) Open the browser try{toast.maketext (pocketsphinxdemo      . This, "Starting browser, please later ...", Toast.length_short). Show (); Uri uri=uri.parse ("http://www.baidu.com"); Converts a string to a URI object Intent intent2=new Intent (Intent.action_view,uri);//Create a Intent intent2.addfla that specifies both the ACTION property and the Data property      GS (INTENT.FLAG_ACTIVITY_NEW_TASK); StartActivity (Intent2); Start Activity}catch (Activitynotfoundexception e) {toast.maketext (pocketsphinxdemo.this, "Launch ' Browser ' Exception!     \ n Please check if the app is installed. ", Toast.length_short). Show ();      } (3) Open the map try{toast.maketext (pocketsphinxdemo.this, "opening map, please later ...", Toast.length_short). Show (); Uri Uri=uri.parse ("geo:38.899533,-77.036476");//convert String to 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, "Launch ' map ' exception!)     \ n Please check if the app is installed. ", Toast.length_short). Show ();     } (3) Edit SMS (call to send SMS program) toast.maketext (pocketsphinxdemo.this, "Opening SMS, please later ...", Toast.length_short). Show ();     Intent intent4=new Intent (Intent.action_view);//Create a Intent intent4.settype ("Vnd.android-dir/mms-sms") with the ACTION attribute; StartActivity (INTENT4);(4) View Contact Toast.maketext (pocketsphinxdemo.this, "starting contact, please later ...", Toast.length_short). Show (     );     Intent intent5 = new Intent (Intent.action_view, ContactsContract.Contacts.CONTENT_URI);      StartActivity (INTENT5);(5) Open camera Toast.maketext (pocketsphinxdemo.this, "Starting camera, please later ...", Toast.length_short). Show ();     Intent intent7=new Intent (); Intent7.setaction (Mediastore.Intent_action_still_image_camera)///Start Camera app startactivity (INTENT7);(6) Open the Gallery Toast.maketext (Pocketsphinxdemo.this,      "Opening gallery, please later ...", Toast.length_short). Show ();     Intent intent8 = new Intent ();     Intent8.settype ("image/*");     Intent8.setaction (intent.action_get_content); StartActivity (intent8);(7) Open the calculator toast.maketext (pocketsphinxdemo.this, "starting calculator, please later ...", Toast.length_short). Show (     );     Intent intent11 = new Intent (); Intent11.setclassname ("Com.android.calculator2", "com.android.calculator2.Calculator");// Call Setclassname specifies which application to start startactivity (intent11);(8) Open System settings Intent intentset= new Intent (settings.action_settings     );              StartActivity (Intentset);(9) Turn on clock try{Intent intentclock=new Intent ();              Intentclock.setclassname ("Com.android.deskclock", "Com.android.deskclock.DeskClock");     StartActivity (Intentclock); } catch (Activitynotfoundexception e) {toast.maketext (Pocketsphinxdemo.this, "Start ' clock ' xorOften!     \ n Please check if the app 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, "Start File Manager" Exception!)     \ n Please check if the app is installed. ", Toast.length_short). Show ();      } (11) Open qqtry{Toast.maketext (pocketsphinxdemo.this, "opening QQ chat tool, please later ...", 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 (Pocketsphinxdemo.this, "Start ' QQ ' Exception!  \ n Please check if the app is installed. ", Toast.length_short). Show ();      } (12) Open try{Toast.maketext (pocketsphinxdemo.this, "Starting client, please later ...", 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, "Start" Exception!)  \ n Please check if the app is installed. ", Toast.length_short). Show ();  } (13) Restart phone String cmd = "su-c reboot";           try {toast.maketext (pocketsphinxdemo.this, "Restarting the phone, please later ...", Toast.length_short). Show ();     Runtime.getruntime (). exec (CMD); } catch (IOException e) {//TODO auto-generated catch block new Alertdialog.builder (Pocketsphinxdemo . This). Settitle ("Error"). Setmessage (E.getmessage ()). Setpositivebutton ("OK", null                 ). Show (); }

Android Note nine. Intent exception handling and common system calls

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.