"Android" uses intent to call system other program, use OnKeyDown to listen to volume key, long press event

Source: Internet
Author: User

Intent is very common in Android programming, and it has been shown in "Android" to use bundles to transfer values between multiple activity (clicking the Open link) to wake up other activity and pass data between the activity. In fact, the role of intent is far from this, it can also invoke other inherent programs in the system, such as making calls, sending text messages and so on. OnKeyDown is also the case, not only can the device's menu key to listen, this in the "Android" a variety of pop-up boxes and the menu key, the Return key monitoring (click the Open link) has been shown, the device's volume adjustment keys can also be monitored, Here are a few examples to illustrate these two issues:


In this program, enter the number to send the telecommunications, to send the SMS, you can call the system's text message screen to send text messages. Due to the inherent limitations of the Android system, it must be the user click the Send button to further the letter, just send the content, the sender has been for the user, according to the user's own fill in the information completed.

For example, there are two Android simulators, 5556 can send a aaasss sms to 5554.

After that, the volume app listens and pops up a message.

At the same time, I press and hold the "long press Back to desktop" button to return to the desktop.

This is the same call here, but the simulator does not call this component, here can not be displayed.

1, the first res\values\strings.xml in each character file as follows, nothing special:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name=    "app_name" >intent application </ string>    <string name= "action_settings" >Settings</string>   <string name= "Button1" > Long press Back to desktop </string>      <string name= "Textview1" > Phone:</string>      <string name= "Button2" > Call </string>    <string name= "textview2" > Send SMS Phone number:</string>        <string name= "TEXTVIEW3" > Send SMS:</string>        <string name= "Button3" > Send sms </string>  </resources>
2, after the layout of the mainactivity, layout ideas are as follows, buttons, linear layout, etc. have been to the hem, in the horizontal layout, with the "Android" using the relative layout of the update software style for the Theme dialog box activity, the use of Layout_ The weight attribute divides the scale of the table layout (click the Open link), which is not described here.


So the code for Res\layout\activity_main.xml is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent"         android:layout_height= "match_parent" android:orientation= "vertical" > <button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/butto N1 "android:textsize=" 24sp "/> <linearlayout android:layout_width=" Match_parent "Android:lay out_height= "wrap_content" android:orientation= "horizontal" > <textview android:layout_width = "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@str            Ing/textview1 "android:textsize=" 24sp "/> <edittext android:id=" @+id/edittext1 "            Android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "3" Android:inputtype= "TexT "android:textsize=" 24sp "/> </LinearLayout> <button android:id=" @+id/button2 "        Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/button2" Android:textsize= "24sp"/> <linearlayout android:layout_width= "Match_parent" Android:layout_hei             ght= "wrap_content" android:orientation= "horizontal" > <textview android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/tex Tview2 "android:textsize=" 24sp "/> <edittext android:id=" @+id/edittext2 "and Roid:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" a Ndroid:inputtype= "text" android:textsize= "24sp"/> </LinearLayout> <linearlayout Andro Id:layout_width= "Match_pArent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <textview            Android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/textview3" android:textsize= "24sp"/> <edittext android:id = "@+id/edittext3" android:layout_width= "0dip" android:layout_height= "Wrap_content" Androi    d:layout_weight= "1" android:inputtype= "text" android:textsize= "24sp"/> </LinearLayout> <button android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "WR Ap_content "android:text=" @string/button3 "android:textsize=" 24SP "/></linearlayout>
3, followed by Mainactivity.java in the code, the same is divided into two parts, part of the events of the button, the button's long press event will use the Setonlongclicklistener listener. The other part is the monitoring of the physical button, and the different key code changes are the events of the different physical buttons being pressed. With intent, you can easily activate each of your own apps and activity, and this shows you how to invoke a program inherent in your system. When activating the program, with the corresponding information, the system will automatically populate the corresponding columns, such as the recipient of text messages. So the code looks like this:

Package Com.intent;import Android.net.uri;import Android.os.bundle;import android.view.keyevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onlongclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;import android.app.Activity; Import Android.content.intent;public class Mainactivity extends Activity {private button button1;private button button2; Private Button button3;private EditText edittext1;private EditText edittext2;private EditText edittext3;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//Get individual components button1 = (Button) Findviewbyid (r.id.button1); button2 = (Button) Findviewbyid ( R.id.button2); Button3 = (Button) Findviewbyid (r.id.button3); editText1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); EditText2 = (EditText) Findviewbyid (r.id.edittext2); editText3 = (EditText) Findviewbyid (R.ID.EDITTEXT3);// Long press to return to desktop button1.Setonlongclicklistener (New Onlongclicklistener () {@Overridepublic Boolean onlongclick (View arg0) {Intent Intent = new Intent (); intent.setaction (Intent.action_main); intent.addcategory (intent.category_home); startactivity (Intent); return false;}); /Calls to call program Button2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {String PhoneNumber = Edittext1.gettext () + ""; Intent Intent = new Intent (); intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" + phonenumber)); Toast.maketext (Mainactivity.this, "Start calling! ", Toast.length_long). Show (); startactivity (intent);}); /Call to send SMS program Button3.setonclicklistener (new Onclicklistener () {public void OnClick (View arg0) {String PhoneNumber = Edittext2.gettext () + ""; String message = Edittext3.gettext () + ""; Intent Intent = new Intent (); intent.setaction (intent.action_sendto); Intent.setdata (Uri.parse ("Smsto:" + PhoneNumber)); Intent.putextra ("Sms_body", message); Toast.maketext (Mainactivity.this, "Start sending SMS! ", Toast.length_lonG). Show (); startactivity (intent);}}); Monitoring of physical Buttons @overridepublic boolean onKeyDown (int keycode, keyevent event) {switch (keycode) {case Keyevent.keycode_ VOLUME_UP:Toast.makeText (Mainactivity.this, "volume increased! ", Toast.length_long). Show (); Break;case KeyEvent.KEYCODE_VOLUME_DOWN:Toast.makeText (Mainactivity.this," Volume down! ", Toast.length_long). Show (); break;} Return Super.onkeydown (KeyCode, event);}}

4, because this program can send text messages, make calls, we also need to write these two permissions in the Androidmanifest.xml. So Androidmanifest.xml modified as follows.

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.intent "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk Andro id:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Android.permission.CA Ll_phone "/><!--get permission to make a call--<uses-permission android:name=" Android.permission.SEND_SMS "/><!-- Get permission to send information--<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" a Ndroid:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "c Om.intent.MainActivity "android:label=" @string/app_name "> <intent-filter> & Lt;action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.categor Y.launcher "/> </intent-fIlter> </activity> </application></manifest> 


"Android" uses intent to call system other program, use OnKeyDown to listen to volume key, long press event

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.