Activity的2种启动方式

目录

直接启动activity
带有返回值的activity启动
显式Intent启动activity
隐式Intent启动activity
Intent六大属性
包含显式启动 打电话 发短信 开网页 传数据 隐式启动代码例程

直接启动activity

直接启动activity方式比较简单,仅需要创建intent再使用startactivity启动即可

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);

带有返回值的activity启动

显式Intent启动activity

不带有返回值的显示Intent启动activity格式:

Intent intent=new Intent()
startActivity(intent)

带有返回值的显示Intent启动activity格式:
Different

Intent intent=new Intent()
startActivityForResult(Intent, requstcode);

具体代码如下:
1第一个界面调用statrtActivityForResult(intent,requestCode)
2第二个界面调用getIntent()得到启动的Intent
3关闭第二个界面之前先封装数据 intent.putExtra(“ ”,”“);
setCode(RESULT_OK,intent);
4关闭界面SecondActivity.this.finish();
5第一个界面调用onActivityResult接收数据

    private TextView et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.mylinearlayout);
        Button button = (Button) findViewById(R.id.bt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(intent, 0x11);
            }
        });
    }
//第二个界面执行setResult(RESULT_OK,intent)后将调用onActivityResult方法
    @Override
    protected void onActivityResult(int 

requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
          String s=data.getStringExtra("seconddata") ;
            et= (TextView) findViewById(R.id.et2);
            et.setText(s);//将s显示到TextView中

        }
    }

SecondActivity:

private Intent intent;
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Button bt= (Button) findViewById(R.id.bt);
        intent=getIntent();//得到调用它的Intent
        bt.setOnClickListener(new 

View.OnClickListener() {
            @Override
            public void onClick(View v) {
            et= (EditText) findViewById(R.id.et);
                        intent.putExtra("seconddata",et.getText().toString());              setResult(RESULT_OK,intent);
                finish();
            }
        });

    }

注意:
不要忘记在manifest中注册activity:

 <activity android:name=".SecondActivity"></activity>

隐式Intent启动activity

格式:
在manife中的 标签下配置

  <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.lingzhuo.test"/>
                 <!--""内的内容随意-->
                <category android:name="android.intent.category.DEFAULT"/>
                <!--必须为default-->
            </intent-filter>

调用时格式

  Intent intent=new Intent("com.lingzhuo.test");
                startActivity(intent);

Intent六大属性

1、action– The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
2、data – The data to operate on, such as a person record in the contacts database, expressed as a Uri.
3、category – Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.
4、type – Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.
5、component – Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
6、extras – This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.

包含显式启动 打电话 发短信 开网页 传数据 隐式启动代码例程

Dial_call——main Activity
在该activity中通过点击不同的按钮实现不同的功能,通过bt_send传递数据给第二个界面。

package com.example.administrator.myfirstlinearlayout;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Administrator on 2015/8/19.
 */
public class Dial_call extends Activity {
    private EditText phonenum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dial_layout);
       phonenum= (EditText) findViewById(R.id.et_phone);
        Button bt_dial= (Button) findViewById(R.id.Dial);//拨号
        Button bt_call= (Button) findViewById(R.id.Call);//直接打电话
        Button bt_msg= (Button) findViewById(R.id.bt_msg);//发短信
        Button bt_invisible= (Button) findViewById(R.id.bt_invisible);//隐式启动
        Button bt_send= (Button) findViewById(R.id.bt_send);//传数据
        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.lingzhuo.test");
                String s=phonenum.getText().toString();
                intent.putExtra(Statics.Intent_key,s);
                startActivity(intent);

            }
        });
        bt_invisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.lingzhuo.test");
                startActivity(intent);
            }
        });

        bt_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(intent.ACTION_SENDTO);
                intent.putExtra("sms_body","hello");
                intent.setData(Uri.parse("smsto:15764240492"));
                startActivity(intent);
            }
        });
        Button bt_web= (Button) findViewById(R.id.bt_web);
        bt_web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
        bt_dial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+phonenum.getText().toString()));
                startActivity(intent);
            }
        });
        bt_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+phonenum.getText().toString()));
                startActivity(intent);
            }
        });
    }

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myfirstlinearlayout"
    >
    <!--请求直接打电话的权限,注意权限小写-->
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.lingzhuo.test"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name=".Dial_call">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter></activity>
    </application>

</manifest>
package com.example.administrator.myfirstlinearlayout;

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

/**
 * Created by Administrator on 2015/8/18.
 */
public class SecondActivity extends Activity {
    private Intent intent;
    private EditText et;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Button bt= (Button) findViewById(R.id.bt);
        textView= (TextView) findViewById(R.id.text);
        intent=getIntent();//得到调用它的Intent
        String s=intent.getStringExtra(Statics.Intent_key).toString();
        textView.setText(s);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et= (EditText) findViewById(R.id.et);
                intent.putExtra("seconddata",et.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
            }
        });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值