The third day android---------implement the corresponding interface

Today, I mainly learned how to implement various interfaces or functional blocks provided by Android in Android, such as making calls and sending text messages, which require permissions and involve system security operations.


Now, to start making calls and sending text messages
, the interface I implemented is as follows (using a table layout): The



buttons can be a little better, but it feels too cumbersome, so I use the button directly. Its definition is as follows:

            <Button
                android:id="@+id/button1"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:onClick="digital_click" //All bind to the same event
                android:text="1" /> //Store the value,

 The events corresponding to the keys are as follows

	 public void digital_click(View view){
		  
		 Button btnDigital=(Button) view;
  	     String text=btnDigital.getText().toString();  
  	     a=a+text;
  	    textResult.setText(a.toString());

  }

 It is simply to read the value of the button, add it to the back of the current string, and display it in the corresponding display box.

 

The events corresponding to the dial buttons are as follows:

	 public void dial(View view) {
			
		 String number=textResult.getText().toString();
		Intent intent = new Intent();
		intent.setAction(intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);//The method will automatically add a category to the Intent: android.intent.category.DEFAULT

		 }

 It mainly gets the currently entered number and calls Intent to make a call. It can be said that this section is the key point. However, before running, the following code must be added to the corresponding counterpart to obtain permission

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

 

 The delete button on the page is to delete the last character of the string, which is relatively simple and will not be explained.

 

The new contact is also a button, and the events bound to it are as follows:

	 public void addContact(View view){  
         Intent it = new Intent(Intent.ACTION_INSERT,Uri.withAppendedPath(Uri.parse("content://com.android.contacts"),"contacts" ));  
         it.setType("vnd.android.cursor.dir/person");  
         String number = textResult.getText().toString(); //Get the number
         it.putExtra(android.provider.ContactsContract.Intents.Insert.SECONDARY_PHONE,number);  
         startActivity(it);  
           
     }  

 Mainly to get the currently entered number, jump and pass the number to the new contact page of the system.

 

As for SMS, I implemented it like this:

 

This is the SMS page (although click send, it is not sent directly, but jumps to the system's sending SMS page), it also implements the interface for sending SMS, the implementation of the interface is as follows:

	 public void send(View view){  
		 
		    String telMsg=message.this.tel.getText().toString();
			String contentMsg=message.this.content.getText().toString();
			
			Uri uri=Uri.parse("smsto:"+telMsg);//Receiver's mobile phone
			Intent it=new Intent();
			
			it.setAction(Intent.ACTION_SENDTO);//Specify action, I want to send text message
			it.putExtra("sms_body", contentMsg);//Set the information content
			it.setType("vnd.android-dir/mms-sms");//Set MIME type
			it.setData(uri);//Set data, where to go	
			message.this.startActivity(it);
           
     }

 When implementing this page, I want the previous page to pass the number that the user may enter to the page, and finally I found such an implementation method on the Internet:

	 public void send_message(View view){ //Bind the event to the button corresponding to the previous page to jump and pass values
		 
		 String number=textResult.getText().toString(); //Get the number
		   Intent intent=new Intent();
	    	intent.setClass(bo_hao.this, message.class);
	    	intent.putExtra(to_you, number); / bind the number to a previously defined variable
	    	bo_hao.this.startActivity(intent);
           
     }  

 And on the SMS page, implement the following code:

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.message);
		
		Intent intent =getIntent(); //equivalent to establishing a connection
		String to_you=intent.getStringExtra(bo_hao.to_you); //Get the value
		
		this.tel=(EditText)super.findViewById(R.id.tel); //Assign the value to the corresponding input box
		tel.setText(to_you);
		this.content=(EditText)super.findViewById(R.id.content);

	}

 
 

 The above is probably what I learned today. The main thing is to know how to implement the Android interface, and the second is to be more proficient in writing Android. If you want to do Android in the future, it is generally relatively simple, but it is time to write code and it is difficult to debug it. If you make a mistake, you will not know where it is wrong, and you must be familiar with various components and interfaces.

 

ps: When I wrote the code today, I just made a simple jump, and everything went wrong. After checking for a long time, I found out that a variable name in the corresponding page has not been changed.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326140678&siteId=291194637