我有回来了啊,今天带来了Android 学习之显式激活与隐式激活Activity

    前段时间由于种种原因,好久没有更新博客了。

    现在俺又回来了。

    哈哈哈,好开心。。。

   下面给大家演示个。。。。

Android 学习之显式激活与隐式激活Activity

在res界面里面有两个布局文件activity_main和acivity_two

activity_main里面有如下四个按钮

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.twoactivity.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第一个界面" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="跳转到第二个界面" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="激活系统的一个应用" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click3"
        android:text="隐式意图跳转到第二个界面" />
    
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click4"
        android:text="激活短信的界面" />

</LinearLayout>
复制代码

acivity_two.xml

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.twoactivity.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个界面" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_menu_dropdown_panel_holo_dark" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="跳转到第二个界面" />

</LinearLayout>
复制代码

现在我们需要点击main里面的"跳转到第二个页面",让程序显示地跳转到two页面里面

下面我们在src下面建立两个类,分别继承Activity类

MainActivity.java

复制代码
package com.example.twoactivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // 当用户点击按钮的时候跳转到第二个界面
    public void click(View view) {
        // 单击按钮激活第二个界面,跳转
        // Intent 意图
        /*
         * Intent intent = new Intent(); intent.setClassName(this,
         * "com.example.twoactivity.OtherScreenActivity");// 设置这个意图要激活的组件
         * startActivity(intent);
         */
        Intent intent = new Intent(this, OtherScreenActivity.class);
        startActivity(intent);
    }

    public void click2(View view) {
        // 单击按钮激活第二个界面,跳转
        // Intent 意图

        Intent intent = new Intent();
        intent.setClassName("com.android.gallery",
                "com.android.camera.GalleryPicker");// 设置这个意图要激活的组件
        startActivity(intent);

    }

    /**
     * 采用隐式意图激活第三个界面
     * 
     * */
    @SuppressLint("NewApi")
    public void click3(View view) {
        // 单击按钮激活第二个界面,跳转
        // Intent 意图

        Intent intent = new Intent();
        intent.setAction("com.example.xxx");
        intent.addCategory("android.intent.category.DEFAULT");

        // 指定数据类型
        // 如果只要类型没有数据
        // intent.setType("vnd.android.cursor.item/haha");
        // 如果只要数据没有类型
        // intent.setData(Uri.parse("itheima:gagaga"));

        // 如果有类型和数据都有
        intent.setDataAndTypeAndNormalize(Uri.parse("itheima:gagaga"),
                "vnd.android.cursor.item/haha");
        startActivity(intent);
        // 动作数据
        // 打人,打酱油
        // 泡茶,泡妞
        // 泡绿茶,泡红尘,泡乌龙
        // addCategory附加信息

    }

    /**
     * 激活短信的服务
     * 
     * */
    public void click4(View view) {
        Intent intent = new Intent();
        intent.setAction("adnroid.intent.action.SENDTO");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setData(Uri.parse("sms:110"));
        startActivity(intent);
    }
}
复制代码

OtherScreenActivity.java

复制代码
package com.example.twoactivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

/**
 * activity是系统的重要组件之一 操作系统要想找到activity,就必须在清单文件里面配置
 * 
 * */
public class OtherScreenActivity extends Activity {
    // 重写activity的Oncreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        Intent intent = getIntent();// 获取到激活他的意图
        Uri uri = intent.getData();
        String result = uri.toString();
        Toast.makeText(this, "数据是:" + result, 0).show();
    }

}
复制代码

并且在清淡文件里面AndroidManifest.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ce"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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=".ResultActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值