android入门之intent示例

从一个界面跳转到另一个界面,把一个界面的信息传送给另一个界面,就需要用到我们的intent方法,接下来带领大家做一个小示例。

配置信息

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zlg.text2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activity_toast_image" />
        <activity android:name=".FrameActivity" />
        <activity android:name=".myviewActivity" />
        <activity android:name=".LifeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

                <data android:scheme="http" />
            </intent-filter>
        </activity>
        <activity android:name=".IntentActivity">
            <intent-filter>
                <action android:name="zlgapp" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="qlu.com"
                    android:path="/android"
                    android:scheme="ftp" />
            </intent-filter>
        </activity>
        <activity android:name=".subActivity" />

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
              <action android:name="broadcast.com"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

首先创建一个MainActivity,还覆盖了他的所有方法,以观察他的生命周期,携带信息。

package com.example.zlg.text2;//快捷键ctrl+o覆盖类
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LifeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_life);
        Log.d(LifeActivity.class.toString(),"onCreate");
        Button button=(Button)findViewById(R.id.life_button);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(LifeActivity.class.toString(),"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(LifeActivity.class.toString(),"onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(LifeActivity.class.toString(),"onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(LifeActivity.class.toString(),"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(LifeActivity.class.toString(),"onStop");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(LifeActivity.class.toString(),"onDestroy");
    }

    public void switchAct(View v)
    {
        Intent intent=new Intent(this,IntentActivity.class);
        intent.putExtra("key","你好");
        startActivity(intent);
    }
    public void impStart(View v)
    {
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        startActivity(intent);
    }


    public void impStart2(View v)
    {
        Intent intent=new Intent("zlgapp", Uri.parse("ftp://qlu.com/android"));
        startActivity(intent);
    }

    public void sendBroadcast(View v)
    {
        Intent intent=new Intent("broadcast.com");
        intent.putExtra("msg","message");
        sendBroadcast(intent);
    }

    public void startSub(View v)
    {
        Intent intent=new Intent(this,subActivity.class);
        startActivityForResult(intent,1);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1)
        {
            if(resultCode==RESULT_OK){
                Toast.makeText(this, "数据为"+data.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this, "已经取消", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
//onCreate onStart onResume打开Activity
//onPause onStop 切换到别的程序
// onRestart onStart onResume//切换回来
//onPause onStop onDestroy//退出程序

创建一个layout_main.xml

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:text="显式跳传跳转到intentActivity"
    android:id="@+id/life_button"
    android:onClick="switchAct"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/life_button2"
        android:onClick="impStart"
        android:text="隐式跳传跳转到百度首页"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/life_button3"
        android:onClick="impStart2"
        android:text="隐式跳传跳转过滤intentActivity"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取subActivity的返回值跳转到lifeActivity"
        android:onClick="startSub"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播BroadcastReceiver"
        android:onClick="sendBroadcast"
        />
</LinearLayout>

跳转到的IntentActivity,接收信息。

package com.example.zlg.text2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class IntentActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_intent);
        Intent intent=getIntent();
        String str=intent.getStringExtra("key");
        TextView textView=(TextView)findViewById(R.id.intent_tv);
        textView.setText(str);
    }
}

其layout_intent.xml如下

<?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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/intent_tv"/>
</LinearLayout>

跳转后

然后创建一个subActivity

package com.example.zlg.text2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class subActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_sub);
        Button button1=(Button)findViewById(R.id.but1);
        Button button2=(Button)findViewById(R.id.but2);
    }
    public void OK(View v)
    {
        String str=((EditText)findViewById(R.id.ed)).getText().toString();
        Intent result = new Intent();
        result.putExtra("msg",str);
        setResult(RESULT_OK, result);
        finish();
    }
    public void NO(View v)
    {
         finish();
    }
}

其layout_sub.xml为

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ed"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but1"
        android:onClick="OK"
        android:text="确认"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but2"
        android:text="取消"
        android:onClick="NO"/>

</LinearLayout>

在创建一个 MyReceiver 的广播

package com.example.zlg.text2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Toast.makeText(context, "广播已发送,信息为:"+intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
    }
}

[资源下载地址](http://download.youkuaiyun.com/download/u011637078/10050880)
有了这个小示例,我们就可以借此发挥我们的想象了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值