Android基础------Activity基础

应用内Activity的跳转方式

一、通过显式意图跳转,如字面意思一样,跳转的目的地是可以明确看到的

二、通过隐式意图跳转,这种方式的跳转需要我们在AndroidManifest.xml文件中为目的Activity配置Action和Category

模拟账号登录,分别使用俩种方式跳转

这是启动的Activity

package com.example.myapplication.intentinner;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.R;

/**
 * 应用内通过显式和隐式跳转
 */
public class SendMessageActivity extends AppCompatActivity {

    private Button buttonShow;
    private Button buttonHide;
    private EditText editTextAccount;
    private EditText editTextPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    /**
     * 初始化UI
     */
    private void initView() {
        buttonShow = findViewById(R.id.btn_send_show);
        buttonHide = findViewById(R.id.btn_send_hide);
        editTextAccount = findViewById(R.id.edit_text_account);
        editTextPassword = findViewById(R.id.edit_text_password);
    }

    /**
     * 设置监听事件
     */
    private void initListener() {
         //通过显式意图跳转
        buttonShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SendMessageActivity.this, ReceiverShowActivity.class);
                String account = editTextAccount.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(SendMessageActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                String password = editTextPassword.getText().toString().trim();
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(SendMessageActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("account",account);
                intent.putExtra("password",password);
                startActivity(intent);
            }
        });

        //通过隐式意图跳转,需要添加action和category,在AndroidManifest.xml文件对应的Activity中查看
        buttonHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.example.myapplication.LOGIN");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                String account = editTextAccount.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(SendMessageActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                String password = editTextPassword.getText().toString().trim();
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(SendMessageActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("account",account);
                intent.putExtra("password",password);
                startActivity(intent);
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".intentinner.SendMessageActivity">

    <EditText
        android:id="@+id/edit_text_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:maxLength="16"
        android:inputType="text"
        android:digits="qwertyuiopasdfghjklzxcvbnm."
        android:layout_marginTop="10dp"/>

    <EditText
        android:id="@+id/edit_text_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:maxLength="20"
        android:inputType="textPassword"
        android:digits="1234567890."
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/btn_send_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显式意图跳转"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/btn_send_hide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐式意图跳转"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>



</LinearLayout>

下面是显示意图的目的Activity

package com.example.myapplication.intentinner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;

import com.example.myapplication.R;

/**
 * 显示意图接收
 */
public class ReceiverShowActivity extends Activity {

    private TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver_show);
        //通过getIntent()方法获取传过来Intent
        Intent intent = getIntent();
        String account = intent.getStringExtra("account");
        String password = intent.getStringExtra("password");
        textView = findViewById(R.id.text_view_show_result);
        textView.setText("账号为:"+account+",密码为:"+password);
    }
}
<?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:padding="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录成功"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:gravity="center"/>

    <TextView
        android:id="@+id/text_view_show_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_marginTop="15dp"/>
</LinearLayout>

下面是隐式意图的目的Activity

package com.example.myapplication.intentinner;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.example.myapplication.R;

/**
 * 隐示意图接收
 */
public class ReceiverHideActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver_hide);
        //通过getIntent()方法获取传过来Intent
        Intent intent = getIntent();
        String account = intent.getStringExtra("account");
        String password = intent.getStringExtra("password");
        textView = findViewById(R.id.text_view_hide_result);
        textView.setText("账号为:"+account+",密码为:"+password);
    }
}
<?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:padding="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录成功"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:gravity="center"/>

    <TextView
        android:id="@+id/text_view_hide_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_marginTop="15dp"/>
</LinearLayout>

跳转到第三方应用,例如手机的浏览器,电话

打开手机的浏览器

package com.example.myapplication.intentout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myapplication.R;

public class ThirdActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
    }

    /**
     * 显式意图跳转手机浏览器
     * @param view
     */
    public void skipShowIntent(View view){
        Intent intent = new Intent();
        //这里举个例子,因为不同机型的浏览器包名可能不同,请根据具体情况而定
        intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
        startActivity(intent);
    }

    /**
     * 隐式意图跳转手机浏览器
     * @param view
     */
    public void skipHideIntent(View view){
        Intent intent = new Intent();
        //这里举个例子,Action、Category和Package视情况而定
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        startActivity(intent);
    }
}
<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显式意图跳转"
        android:onClick="skipShowIntent"
        android:textSize="20sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐式意图跳转"
        android:onClick="skipHideIntent"
        android:textSize="20sp"/>

</LinearLayout>

跳转后获取数据返回

在第二个界面输入内容后返回到第一个界面并显示

package com.example.myapplication.intentforresult;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.R;

/**
 * 请求结果
 */
public class RequestActivity extends AppCompatActivity {

    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);
        textView = findViewById(R.id.text_view_request);
        button = findViewById(R.id.btn_for_result);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RequestActivity.this,ResponseActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    //获取请求结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String msg = data.getStringExtra("result");
        if(1 == requestCode){
            if(2 == resultCode){
                textView.setText(msg);
            }
        }
    }
}
<?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:id="@+id/text_view_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/btn_for_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="请求数据"/>

</LinearLayout>

输入数据并返回界面

package com.example.myapplication.intentforresult;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.R;

/**
 * 返回结果
 */
public class ResponseActivity extends AppCompatActivity {

    EditText editText;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_response);
        editText = findViewById(R.id.edit_text_response);
        button = findViewById(R.id.btn_back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = editText.getText().toString();
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(ResponseActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("result",msg);
                //设置结果码和数据
                setResult(2,intent);
                //不要忘了调用finish()方法结束当前Activity
                finish();
            }
        });
    }
}
<?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">

    <EditText
        android:id="@+id/edit_text_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回结果"/>

</LinearLayout>

这有俩个例子分别是打电话和照相

打电话是跳转第三方应用,照相不仅调用第三方应用,还用到了数据回传

下面是打电话的代码

package com.example.myapplication.intentwithoperate;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.example.myapplication.R;

/**
 * 调用第三方程序打电话
 */
public class CallPhoneActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_phone);
    }

    public void callPhone(View view){
        Intent intent = new Intent();
        Uri uri = Uri.parse("tel:10086");
        intent.setData(uri);
        intent.setAction("android.intent.action.CALL");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        startActivity(intent);
    }
}
<?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">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拨打电话"
        android:onClick="callPhone"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>

</LinearLayout>

下面是拍照,注意在虚拟机上无法拍照,所以看不到效果,我在真机上测试没有问题的

package com.example.myapplication.intentwithoperate;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapplication.R;

/**
 * 调用相机拍照并返回结果
 */
public class CameraRequestActivity extends AppCompatActivity {

    Button button;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_request);
        button = findViewById(R.id.btn_camera);
        imageView = findViewById(R.id.image_camera);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("android.media.action.IMAGE_CAPTURE");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * Activity.RESULT_OK和Activity.RESULT_CANCELED是安卓系统定义的俩个常量,用于表示相机照相的“√”和“×”
         */
        if(1 == requestCode){
            if(Activity.RESULT_OK == resultCode && data != null){
                //用Bitmap来接收照片
                Bitmap bitmap = data.getParcelableExtra("data");
                imageView.setImageBitmap(bitmap);
            }else if(Activity.RESULT_CANCELED == resultCode){
                Toast.makeText(this,"失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".intentwithoperate.CameraRequestActivity">

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开相机"
        android:textSize="20sp"/>
    <ImageView
        android:id="@+id/image_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

-----仅为自己的学习笔记

发布了29 篇原创文章 · 获赞 3 · 访问量 828

猜你喜欢

转载自blog.csdn.net/weixin_44616792/article/details/104497725