Use of Activity

Basic steps to write an Activity:

1. Define a subclass of the Activity class, SecondActivity

2. Configure the components defined in ActivityManifest.xml

3. Define the layout file activity_second.xml

4. Rewrite Activity's onCreate() to load the layout file

Start an Activtiy

General startup:

         

Start with callback:


How to carry extra data

          intent.putExtra(name,value)

How to get carry extra data

       xxx.intent.getxxxExtra(name)

how to get started intent

             Intent getIntent()

end an Activity

 general end

          finish()

end with result

          setResult(int resultCode , Intent data)

              finish()

Activity life cycle and state


Activity的TaskStack

In Activity, the system uses the taskStack structure to store and manage the Activity objects that are started.

When an application starts, the system will create a corresponding taskStack for it to store and manage the Activity object of the application

Only the Activity at the top of the top task stack can be displayed in the window


Activity的launchMode


standard:

    Standard mode, a new instance is generated each time the startActivity() method is called

singleTop

If there is already an instance at the top of the Activity stack, no new instance will be created

If it is not at the top of the stack, a new instance will be created

singleTask

There is only one instance, by default in the current Task

singleInstance

There is only one instance, a new booth will be created when it is created, and there can be no other objects in this stack

——————————————————————————————————————————————

call text code

MainActivity:

 private EditText et_main_num;

     private EditText et_main_sms;
     private Button btn_main_call;

     private Button btn_main_send;

protected void onCreate(Bundle savedInstanceState){

super.onCreate (savedInstanceState);

setContentView(R.layout.activity_main);

//Initialize the control object. There are many button settings on the page to monitor

et_main_num=(EditText) findViewById(R.id.et_main_num);

et_main_sms=(EditText) findViewById(R.id.et_main_sms);

btn_main_call=(Button) findViewById(R.id.btn_main_call);

btn_main_send=(Button) findViewById(R.id.btn_main_send);

//Set the click listener for the button


btn_main_call.setOnClickListener(onClickListener);

btn_main_send.setOnClickListener(onClickListener);

//Set long press click listener

btn_main_call.setOnLongClickListener(this);

btn_main_send.setOnLongClickListener(this);

}

//click listener

private OnClickListener onClickListener=new View.OnClickListener() {

        public void onClick(View v){

if (v==btn_main_call){//Call

 //1. Create Intent Implicit

String action="android.intent.action.DIAL";

 Intent intent=new Intent(action);

//2. Carry data

 String number=et_main_num.getText().toString().trim();

 intent.setData(Uri.parse("tel:"+number));

//3. Start Activity

startActivity(intent);

}else if(v==btn_main_send){//Send SMS

Intent intent = new Intent(Intent.ACTION_SENDTO);//Create an Intent object

String number= btn_main_send.getText().toString().trim(); //carry data

 String sms=et_main_sms.getText().toString().trim();

intent.setData(Uri.parse("smsto:"+number));//Set data

intent.putExtra("sms_body", sms);

startActivity(intent); //Start Activity

             }

   }

};

//Long press click listener

public boolean onLongClick(View v){

if (v==btn_main_call) {

Intent intent = new Intent(Intent.ACTION_CALL);

String number=et_main_num.getText().toString().trim();

intent.setData(Uri.parse("tel:"+number));

 startActivity(intent);

}else if(v==btn_main_send){

SmsManager smsmanager = SmsManager.getDefault();

String number= btn_main_send.getText().toString().trim();//carry data

 String sms=et_main_sms.getText().toString().trim();

//send messages

smsmanager.sendTextMessage (number, null, sms, null, null);

}

return true;//Indicates that this event has been consumed, and the click event will not be triggered again

        }

}

activit_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<!-- 
   LinearLayout中的   android:orientation="horizontal" 水平排列
                    android:orientation="vertical" 垂直排列
 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tel" />
        <EditText
            android:id="@+id/et_main_num"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:hint="@string/input_num"
           >
        </EditText>


    </LinearLayout>
    
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sms" />
        <EditText
            android:id="@+id/et_main_sms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:hint="@string/input_content"
           >
        </EditText>


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        >
     <Button 
         android:id="@+id/btn_main_call"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:text="@string/call"
         />
     <Button 
         android:id="@+id/btn_main_send"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:text="@string/send"
         />
        
        
        
    </LinearLayout>
    
     
     
</LinearLayout>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">App02_Call_SMS</string>
    <string name="hello_world">Hello world!</string>
    <string name="tel">电话号码:</string>
    <string name="sms">短信内容:</string>
    <string name="call">打电话</string>
    <string name="send">发短信</string>
    <string name="input_num">请输入号码</string>
    <string name="input_content">请输入短信内容</string>
   


</resources>







Guess you like

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