2022-10-16学习内容
1.Activity生命周期
1.1ActStartActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class ActStartActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "ning"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "ActStartActivity onCreate"); setContentView(R.layout.activity_act_start); findViewById(R.id.btn_act_next).setOnClickListener(this); } @Override public void onClick(View v) { startActivity(new Intent(this, ActFinishActivity.class)); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "ActStartActivity onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "ActStartActivity onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "ActStartActivity onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "ActStartActivity onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "ActStartActivity onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "ActStartActivity onRestart"); } }
1.2效果:
2022-10-16 08:09:13.762 5695-5695/com.example.chapter04 D/ning: ActStartActivity onCreate 2022-10-16 08:09:14.350 5695-5695/com.example.chapter04 D/ning: ActStartActivity onStart 2022-10-16 08:09:14.354 5695-5695/com.example.chapter04 D/ning: ActStartActivity onResume 2022-10-16 08:09:43.811 5695-5695/com.example.chapter04 D/ning: ActStartActivity onPause 2022-10-16 08:09:44.800 5695-5695/com.example.chapter04 D/ning: ActStartActivity onStop 2022-10-16 08:09:45.971 5695-5695/com.example.chapter04 D/ning: ActStartActivity onRestart 2022-10-16 08:09:45.981 5695-5695/com.example.chapter04 D/ning: ActStartActivity onStart 2022-10-16 08:09:45.991 5695-5695/com.example.chapter04 D/ning: ActStartActivity onResume
2.Activity启动模式
2.1activity_jump_first.xml
<?xml version="1.0" encoding="utf-8"?> <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/btn_jump_second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到第二个页面" /> </LinearLayout>
2.2activity_jump_second.xml
<?xml version="1.0" encoding="utf-8"?> <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/btn_jump_first" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到第一个页面"/> </LinearLayout>
2.3JumpFirstActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class JumpFirstActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jump_first); findViewById(R.id.btn_jump_second).setOnClickListener(this); } @Override public void onClick(View v) { // 创建一个意图对象,准备跳到指定的活动页面 Intent intent = new Intent(this, JumpSecondActivity.class); // 栈中存在待跳转的活动实例时,则重新创建该活动的实例,并清除原实例上方的所有实例 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }
2.4JumpSecondActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class JumpSecondActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jump_second); findViewById(R.id.btn_jump_first).setOnClickListener(this); } @Override public void onClick(View v) { // 创建一个意图对象,准备跳到指定的活动页面 Intent intent = new Intent(this, JumpFirstActivity.class); // 栈中存在待跳转的活动实例时,则重新创建该活动的实例,并清除原实例上方的所有实例 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }
2.5效果:
2.6activity_login_input.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是登陆验证页面,此处省略了用户名和密码等输入框"/> <Button android:id="@+id/btn_jump_success" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="跳转到登录成功页面"/> </LinearLayout>
2.7activity_login_success.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是登录成功页面,登陆成功后不必返回登录验证页面。请按返回键观察看看"/> </LinearLayout>
2.8LoginInputActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class LoginInputActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_input); findViewById(R.id.btn_jump_success).setOnClickListener(this); } @Override public void onClick(View v) { // 创建一个意图对象,准备跳到指定的活动页面 Intent intent = new Intent(this, LoginSuccessActivity.class); // 设置启动标志:跳转到新页面时,栈中的原有实例都被清空,同时开辟新任务的活动栈 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }
2.9效果:
3.显示Intent和隐式Intent
3.1ActStartActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class ActStartActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "ning"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "ActStartActivity onCreate"); setContentView(R.layout.activity_act_start); findViewById(R.id.btn_act_next).setOnClickListener(this); } @Override public void onClick(View v) { // 1.在Intent的构造函数中指定 // Intent intent = new Intent(this, ActFinishActivity.class); // 2.调用意图对象的setClass方法指定 Intent intent = new Intent(); // intent.setClass(this, ActFinishActivity.class); // 3.调用意图对象的setComponent方法指定 ComponentName component = new ComponentName(this, ActFinishActivity.class); intent.setComponent(component); startActivity(intent); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "ActStartActivity onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "ActStartActivity onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "ActStartActivity onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "ActStartActivity onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "ActStartActivity onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "ActStartActivity onRestart"); } }
3.2activity_action_uri.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="点击以下按钮将向号码12345发起请求"/> <Button android:id="@+id/btn_dial" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到拨号页面" /> <Button android:id="@+id/btn_sms" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到短信页面" /> <Button android:id="@+id/btn_my" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到我的页面" /> </LinearLayout>
3.3ActionUriActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_action_uri); findViewById(R.id.btn_dial).setOnClickListener(this); findViewById(R.id.btn_sms).setOnClickListener(this); findViewById(R.id.btn_my).setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(); String phoneNo = "12345"; switch (v.getId()) { case R.id.btn_dial: // 设置意图动作为准备拨号 intent.setAction(Intent.ACTION_DIAL); // 声明一个拨号的Uri Uri uri = Uri.parse("tel:" + phoneNo); intent.setData(uri); startActivity(intent); break; case R.id.btn_sms: // 设置意图动作为发短信 intent.setAction(Intent.ACTION_SENDTO); // 声明一个拨号的Uri Uri uri2 = Uri.parse("smsto:" + phoneNo); intent.setData(uri2); startActivity(intent); break; case R.id.btn_my: intent.setAction("android.intent.action.NING"); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); break; } } }
3.4charpter03/manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chapter03"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".CalculatorActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.NING" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
3.5charpter04/manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chapter04"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".LoginSuccessActivity" android:exported="false" /> <activity android:name=".JumpSecondActivity" android:exported="false" /> <activity android:name=".ActFinishActivity" android:exported="false" /> <activity android:name=".ActionUriActivity" android:exported="true" android:launchMode="standard"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
3.6效果: