Activity生命周期

package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

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

public class ACtStartActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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));

    }
}
package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class ACtFinishActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_finish);
        ImageButton viewById = findViewById(R.id.iv_back);
        viewById.setOnClickListener(this);
        findViewById(R.id.btn_finish).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        finish();
    }
}

1.生命周期

 

2.Activity启动模式

默认启动模式 

 

 

 

 xml 和 Java结合使用

3. 显示和隐式Intent

 

package com.tiger.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) {
        String phoneNo="123456";
        if (v.getId() == R.id.btn_dial){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            Uri uri = Uri.parse("tel:"+phoneNo);
            intent.setData(uri);
            startActivity(intent);
        }else if (v.getId() == R.id.btn_sms){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SENDTO);
            Uri uri = Uri.parse("smsto:"+phoneNo);
            intent.setData(uri);
            startActivity(intent);
        }else {

            Intent intent = new Intent();
            intent.setAction("android.intent.action.JMJ");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }



    }
}

 4. 向下一个Activity发送消息

发送 

package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Date;

public class SendActivity extends AppCompatActivity implements View.OnClickListener{
   private TextView tv_send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_send = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ReceiveActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_time",new Date().toString());
        bundle.putString("request_content",tv_send.getText().toString());

        intent.putExtra("bundle",bundle);
        startActivity(intent);
    }
}

接收

package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ReceiveActivity extends AppCompatActivity {
    TextView tv_receive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        tv_receive = findViewById(R.id.tv_receive);

        //从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getBundleExtra("bundle");
        String requestTime = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");

        String desc = requestTime+":"+request_content;
        tv_receive.setText(desc);
    }
}

package com.tiger.chapter04;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Date;

public class SendActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_send;
    ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_send = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null) {
                    Intent data = result.getData();
                    if (data != null && result.getResultCode() == Activity.RESULT_OK) {
                        Bundle extras = data.getExtras();
                        String responseTime = extras.getString("response_time");
                        String response_content = extras.getString("response_content");

                        String desc = responseTime + ":" + response_content;

                        tv_send.setText(desc);


                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ReceiveActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_time", new Date().toString());
        bundle.putString("request_content", tv_send.getText().toString());

        intent.putExtras(bundle);
        register.launch(intent);

    }
}

 

package com.tiger.chapter04;

import androidx.activity.result.ActivityResult;
import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Date;

public class ReceiveActivity extends AppCompatActivity implements View.OnClickListener{
    TextView tv_receive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        tv_receive = findViewById(R.id.tv_receive);

        //从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        String requestTime = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");

        String desc = requestTime+":"+request_content;
        tv_receive.setText(desc);

        findViewById(R.id.btn_response).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time",new Date().toString());
        bundle.putString("response_content","是啊,今天天气真好,很不错");
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK,intent);
        finish();

    }
}

5.利用资源文件的字符串

package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ReadStringActivity extends AppCompatActivity {

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

        TextView tv_resource = findViewById(R.id.tv_resource);
        //Activity 继承 Context 上下文

        //从strings.xml 获取名叫 weather_str 的字符串值
        String value = getString(R.string.weather_str);

        tv_resource.setText(value);
    }
}

6.利用元数据传递消息 

package com.tiger.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ReadStringActivity extends AppCompatActivity {

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

        TextView tv_resource = findViewById(R.id.tv_resource);
        //Activity 继承 Context 上下文

        PackageManager packageManager = getPackageManager();
        try {
            //获取Activity信息 拿到当前上下文的组件名字 获取元数据信息 从应用包管理器中获取当前的活动信息
            ActivityInfo info = packageManager.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            //获取活动附加的元数据信息
            Bundle metaData = info.metaData;
            String jmj = metaData.getString("jmj");
            tv_resource.setText(jmj);
            //因为可能调用第三方Activity 需要用Token作验证 放在第三方Activity上 的元数据 他们去做校验。


        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }



    }
}

7.给应用页面注册快捷方式

<?xml version="1.0" encoding="utf-8"?>
<!--快捷方式-->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/f"
        android:shortcutLongLabel="@string/f">

        <intent android:action="android.intent.action.VIEW"
            android:targetPackage="com.tiger.chapter04"
            android:targetClass="com.tiger.chapter04.ACtStartActivity"/>
        <categories android:name = "android.shortcut.conversation"/>

    </shortcut>

    <shortcut android:shortcutId="second"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/s"
        android:shortcutLongLabel="@string/s">

        <intent android:action="android.intent.action.VIEW"
            android:targetPackage="com.tiger.chapter04"
            android:targetClass="com.tiger.chapter04.SendActivity"/>
        <categories android:name = "android.shortcut.conversation"/>

    </shortcut>

    <shortcut android:shortcutId="third"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/t"
        android:shortcutLongLabel="@string/t">

        <intent android:action="android.intent.action.VIEW"
            android:targetPackage="com.tiger.chapter04"
            android:targetClass="com.tiger.chapter04.ActionUriActivity"/>
        <categories android:name = "android.shortcut.conversation"/>

    </shortcut>

</shortcuts>

 

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值