Intent的学习笔记

来源:互联网 发布:软件测试项目心得体会 编辑:程序博客网 时间:2024/05/16 11:01

Intent的常用方法依次如下:
1.向上一个活动返回消息
2.使用显示Intent进行活动中的跳转
3.使用隐式Intent进行活动间的跳转,并用Intent向跳转到的活动传递信息
4.打开网页
5.直接拨打电话
6.跳转到拨电话的界面并且已经录入要打的电话的号码7.发送广播
8.使用隐式Intent判断有没有匹配的Activity
(注意:单纯的Activity在Mainifest.xml中的注册省略未写出

这里写图片描述

**1.向上一个活动返回消息**:从第一个Activity到第二个Activity后,按退出键,使用Intent可以将被销毁Activity(第二个Activity)的信息传给一个活动:

在FrstActivity中写下如下代码:

// 向上一个活动返回消息
public void click2(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra(“name”, str);
startActivityForResult(intent, 1);
}

@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {        case 1:            if (resultCode == RESULT_OK) {                String str = data.getStringExtra("name");                toast(str);            }            break;        default:        }

在FirstActivity对应的布局first.xml中写下如下代码:

 <Button         android:text="去第二界面(第二界面可以返回信息)"        android:onClick="click2"        android:layout_height="wrap_content"        android:layout_width="match_parent"/>

在第二界面的活动SecondActivity中写下代码:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.second_layout);        Intent intent = new Intent();        intent.putExtra("name", "Hello FirstActivity!");        setResult(RESULT_OK, intent);    }

在SecondActivity时按返回键会在FirstActivity中收到消息,这是SecondActivity向FirstActivity传递的信息:

这里写图片描述

2.使用显示Intent进行活动中的跳转:
在FrstActivity中写下如下代码:

// 显示Intent    public void click3(View v) {        Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);        startActivity(intent);    }

在first.xml中加入:

<Button         android:text="去第三界面(使用显示Intent)"        android:onClick="click3"        android:layout_height="wrap_content"        android:layout_width="match_parent"/>

去第三界面:
这里写图片描述

3.使用隐式Intent进行活动间的跳转,并用Intent向跳转到的活动传递信息:
在FrstActivity中写下如下代码,这里”com.example.tsst19.MY_ACTION”和”com.example.tsst19.MY_CATEGORY是自定义的,需要在AndroidManifest.xml进行自定义:

// 隐式Intent,并向下一个活动传递消息    public void click4(View v) {        Intent intent = new Intent("com.example.tsst19.MY_ACTION");        intent.addCategory("com.example.tsst19.MY_CATEGORY");        intent.putExtra("name2", "你好,第四界面");        startActivity(intent);    }

在first.xml中加入:

<Button         android:text="去第四界面(使用隐式Intent,并传递信息)"        android:onClick="click4"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>

AndroidManifest.xml中加入以下代码:

 <activity android:name=".FourthActivity">            <intent-filter >                <action android:name="com.example.tsst19.MY_ACTION"/>                <category android:name="android.intent.category.DEFAULT"/>                <category android:name="com.example.tsst19.MY_CATEGORY"/>            </intent-filter>        </activity>

4.打开网页,例如百度首页:
FirstActivity中加入代码:

// 打开网页    public void webClick(View v) {        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.setData(Uri.parse("http://www.baidu.com"));        startActivity(intent);    }

first.xml加入如下代码:

<Button         android:text="打开网页(百度首页)"        android:onClick="webClick"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>  

点击按钮后的结果:
这里写图片描述

5.直接拨打电话,例如1008611:
需要打电话的权限,在AndroidManifest.xml中加入:

<uses-permission android:name="android.permission.CALL_PHONE"/>

在FirstActivity中加入代码:

// 直接打电话    public void callClick(View v) {        Intent intent = new Intent(Intent.ACTION_CALL);        intent.setData(Uri.parse("tel:1008611"));        startActivity(intent);    }

在first.mxl中加入:

<Button         android:text="直接打电话(1008611)"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="callClick"/>

点击按钮后的效果:
这里写图片描述

6.跳转到拨电话的界面并且已经录入要打的电话的号码:(以10086为例)
(注意:此处不需要拨打电话的权限,与直接拨打电话的区别是在FirstActivity中的Intent.ACTION_CALL与Intent.ACTION_DIAL)
FirstActivty加入代码:

// 去打电话界面,准备打电话    public void dailClick(View v) {        Intent intent = new Intent(Intent.ACTION_DIAL);        intent.setData(Uri.parse("tel:10086"));        startActivity(intent);    }

first.xml中加入代码:

<Button         android:onClick="dailClick"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="去电话界面 (准备拨打10086)"/>

按下按钮后的效果:
这里写图片描述

7.发送广播:
在FirstActivty中加入代码:

// 广播    public void broadcastClick(View v) {        Intent intent = new Intent("com.example.tsst19.MY_BROADCAST");        intent.putExtra("data", "这是一条广播");        sendBroadcast(intent);    }

first.xml中加入:

<Button         android:onClick="broadcastClick"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发送一条广播"/>

新建一个广播接受器:类 MyBroadcast,接受广播,并输出接受到的消息:

package com.example.tsst19;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyBroadcast extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String str = intent.getStringExtra("data");        Toast.makeText(context, "接受到广播信息:\n" + str, Toast.LENGTH_SHORT).show();    }}

在Mainifest.xml中对广播接受器进行注册:

<receiver android:name=".MyBroadcast">            <intent-filter >                <action android:name="com.example.tsst19.MY_BROADCAST"/>            </intent-filter>        </receiver>

按下按钮后的结果:
这里写图片描述


8.使用隐式Intent判断有没有匹配的Activity的代码片段,添加这些就不会在担心因为没有不匹配的Activity而程序崩溃了:

        Intent intent=new Intent();        intent.setAction("com.android.activity.MY_ACTION" );        intent.addCategory("com.android.activity.htw2");        PackageManager packageManager = getPackageManager();        List<ResolveInfo> activitis = packageManager.queryIntentActivities(intent, 0);        boolean isIntentSafe = activitis.size()>0;        if(isIntentSafe)        startActivity(intent);
0 0
原创粉丝点击