Android之Intent和Activity

Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了。Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助。在Android的官方API文档里边对Intent是这样定义的:An Intent is an abstract description of an operation to be performed。一个Intent就是一次对将要运行的操作的抽象描写叙述(真实够抽象的)。

详细有一下3种形式:
1、通过startActivity方法来启动一个新的Activity。
2、通过Broadcast机制能够将一个Intent发送给不论什么对这个Intent感兴趣的BroadcastReceiver。
3、通过startService(Intent)或bindService(Intent, ServiceConnection, int)来和后台的Service交互。



以下来看一下怎样通过startActivity方法启动一个新的Activity。
Intent最经常使用的用途就是连接一个应用其中的各个Activity,假设我们把Activity比作积木的话。那么Intent就好像是胶水。把不同的积木粘起来,构成我们搭建的房子。在程序其中假设要启动一个Activity的话,通常我们会调用startActivity方法,并把Intent作为參数传进去。例如以下所看到的:
startActivity(intent);
这个Intent或者指定了一个Activity,或者里边仅仅包括了选定Activity的信息。可是详细启动哪个Activity是由系统去决定的,Android系统负责挑选一个最满足匹配挑选条件的Activity。

实例:IntentDemo
执行效果:


代码清单:
AndroidManifest.xml

<?

xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainsong.intentdemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="OtherActivity" android:label="OtherActivity"> </activity> </application> </manifest>

布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到还有一个Activity"
    />
</LinearLayout>
布局文件:other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="OtherActivity"
    />
</LinearLayout>
Java源码文件:MainActivity.java
package com.rainsong.intentdemo;

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

public class MainActivity extends Activity
{
    OnClickListener listener1 = null;
    Button button1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        listener1 = new OnClickListener() {
            public void onClick(View v) {
                Intent  intent1= new Intent(MainActivity.this, OtherActivity.class);
                startActivity(intent1);
            }
        };

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(listener1);
    }
}

Java源码文件:OtherActivity.java

package com.rainsong.intentdemo;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 程序设计中,IntentActivity 是非常常见的两个概念,它们分别用于实现不同的功能。 1. Intent Intent 是用于在 Android 应用程序之间传递数据的一种机制。通过 Intent,我们可以实现 Activity 之间的数据传递、启动系统组件、启动服务等操作。Intent 由两部分组成:Action 和 Data。其中,Action 表示要执行的操作,如打开某个 Activity、发送邮件等;Data 则描述了要操作的数据,如要打开的文件、要发送的邮件地址等。 下面是一个简单的 Intent 示例代码: ```java Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("name", "Tom"); startActivity(intent); ``` 上述代码中,我们创建了一个 Intent 对象,并将要跳转的 Activity 指定为 SecondActivity。同时,我们还通过 putExtra() 方法向目标 Activity 传递了一个名为 "name" 的参数,值为 "Tom"。最后,我们调用 startActivity() 方法启动目标 Activity。 2. Activity ActivityAndroid 应用程序中的一个组件,用于展示用户界面。每个 Activity 都拥有自己的布局和生命周期,并且可以通过 Intent 启动其他 Activity。 下面是一个简单的 Activity 示例代码: ```java public class SecondActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); textView = findViewById(R.id.textView); Intent intent = getIntent(); String name = intent.getStringExtra("name"); textView.setText("Hello, " + name); } } ``` 上述代码中,我们创建了一个名为 SecondActivityActivity,并在 onCreate() 方法中设置了其布局文件为 activity_second.xml。同时,我们还通过 getIntent() 方法获取了传递过来的 Intent 对象,并从中获取了名为 "name" 的参数值。最后,我们将参数值设置到了 TextView 控件中。 以上就是 IntentActivity 的简单使用方法。在实际开发中,它们还有很多高级用法,如启动 Activity 后获取返回值、使用 Intent 进行隐式调用等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值