现在的位置: 首页 > 综合 > 正文

Intent

2019年07月19日 ⁄ 综合 ⁄ 共 3980字 ⁄ 字号 评论关闭

 

 

 

在一个项目之中,会由多个Activity程序所组成,而这些程序需要依靠Intent完成

各个Activity程序之间的通讯。

  以后只要是增加了新的Activity程序都要在AndroidManifest.xml文件中进行配置。

 

 

 

需要两个布局文件,一个为send_main.xml,用于发送Intent界面;另一个为receive_main.xml,用于接收Intent界面。

需要两个Activity程序,一个为Send.java,用于发送Intent;另一个为Receive.java,用于接收Intent。

 

 

在res/values/strings.xml中:

 

<resources>

    <string name="app_name">Intent操作</string>

    <string name="hello_world">Hello world!</string>

    <string name="menu_settings">Settings</string>

    <string name="title_activity_send">Send</string>

    <string name="send_name">发送Intent的Activity程序</string>

    <string name="receive_name">接收Intent的Activity程序</string>

</resources>

 

 

 

 

 

在send_main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:gravity="center_horizontal">

  <Button

      android:id="@+id/mybut"

      android:layout_marginTop="8dp"

      android:layout_width="180dp"

      android:layout_height="40dp"

      android:background="#3399ff"

      android:textColor="#ffffff"

      android:text="@string/send_name"/>

</LinearLayout>

 

 

 

 

 

 

在receive_main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000">

  <TextView

      android:id="@+id/show"

      android:gravity="center_horizontal"

      android:layout_marginTop="8dp"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:textSize="30dp"

      android:textColor="#ffffff"/>

</LinearLayout>

 

 

 

 

 

在Send.java中:

 

package com.li.intentproject;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.support.v4.app.NavUtils;

 

public class Send extends Activity {

  private Button mybut = null;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.send_main);

        this.mybut = (Button)super.findViewById(R.id.mybut);

        this.mybut.setOnClickListener(new OnClickListenerImpl());

    }

    private class OnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       Intent it = new Intent(Send.this,Receive.class); //要跳转的名称

       it.putExtra("myinfo","骐骥一跃,不能十步;"+"\n"+"驽马十驾,功在不舍。");     //要穿的值

       Send.this.startActivity(it);  //进行跳转

     }

    }

}

 

 

 

 

 

 

在Receive.java中:

 

package com.li.intentproject;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TextView;

import android.support.v4.app.NavUtils;

 

public class Receive extends Activity {

  private TextView show = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.receive_main);

        this.show = (TextView)super.findViewById(R.id.show);

        Intent it = super.getIntent(); //取得当前的Intent

        String info = it.getStringExtra("myinfo"); //取得数据

        this.show.setText(info);

    }

}

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.intentproject"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".Send"

            android:label="@string/send_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="Receive"

            android:label="@string/receive_name">

        </activity>

    </application>

 

</manifest>

 

 

抱歉!评论已关闭.