1、跳转到一个新的Actitity

新建项目, 新建一个java类OtherScreenActivity 继承自 Activity类

package com.wuyudong.twoactivity;

import android.app.Activity;
import android.os.Bundle; //activity是系统的重要组件
//OS要想找到activity 就必须在清单文件中配置
public class OtherScreenActivity extends Activity { //重写activity的onCreate方法 方法里面设置初始化程序的界面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
} }

显然需要新建一个名为activity_two的android.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:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> <ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

MainActivity.java中的代码如下

package com.wuyudong.twoactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} //当用户点击按钮的时候跳转到第二个页面
public void click(View view) {
Intent intent = new Intent();
intent.setClassName(this, "com.wuyudong.twoactivity.OtherScreenActivity");
startActivity(intent);
}
}

当然,click中的代码还可以改成下面的形式:

    //当用户点击按钮的时候跳转到第二个页面
public void click(View view) {
//Intent intent = new Intent();
//intent.setClassName(this, "com.wuyudong.twoactivity.OtherScreenActivity");
Intent intent = new Intent(this, OtherScreenActivity.class);
startActivity(intent);
}

清单文件AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wuyudong.twoactivity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:icon="@drawable/icon1"
android:name="com.wuyudong.twoactivity.MainActivity"
android:label="@string/activity01" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:icon="@drawable/icon2"
android:name="com.wuyudong.twoactivity.OtherScreenActivity"
android:label="@string/activity02" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

其中drawable是新建在res文件夹下的文件夹,icon1和icon2是放进去的图片

activity_main.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=".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="跳转到第二个界面" /> </LinearLayout>

最终界面如下:

点击按钮后跳转到第二个页面

2、激活系统的应用程序的界面

对于intent.setClassName((String packageName, String className);

当我们不知道应用的packageName和className的时候,点击app图标,查看logcat日志信息,下面是我点击图库图标后所显示的信息:

07-11 09:31:44.153: I/ActivityManager(1227): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery/com.android.camera.GalleryPicker} from pid 1512

添加相应的按钮事件代码如下:

    //当用户点击按钮的时候激活图库的应用
//com.android.gallery/com.android.camera.GalleryPicker
public void click2(View view) {
Intent intent = new Intent();
intent.setClassName("com.android.gallery", "com.android.camera.GalleryPicker");
startActivity(intent);
}

这样就实现了激活系统的应用程序的界面

下面来实践一下,设计一个场景:当连接网络失败的时候跳转到系统的网络界面

代码如下:

package com.wuyudong.testnetwork;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 检查用户的网络情况
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null && info.isConnected()) {
Toast.makeText(this, "网络可用", 0).show();
} else {
Toast.makeText(this, "网络不可用", 0).show();
//定向用户到系统网络的界面
Intent intent = new Intent();
//07-11 15:11:47.488: I/ActivityManager(859):
//Starting: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.WirelessSettings } from pid 1293
intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
startActivity(intent);
} } }

上面代码只能在2.3版本上运行,4.3会报错,因为4.3里面的“com.android.settings.WirelessSettings”发生修改

上面的代码的耦合性比较强,因为setClassName里面的参数必须知道

Android 显示意图激活另外一个Actitity的更多相关文章

  1. [android] 显示意图激活另外一个activity

    可以使用跳转的方式类似javaweb来实现界面转换 显示意图就是必须要指定开启组件的具体信息,包名,组件名,组件的class 新建一个类TwoActivity ,继承Activity类,重写onCre ...

  2. Android 隐式意图激活另外一个Actitity

    上篇文章<Android 显示意图激活另外一个Actitity>最后谈到显示意图激活另外一个Actitity会有一些局限性和弊端 本文介绍另一种方法:隐式意图激活另外一个Actitity ...

  3. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  4. android 显示意图

    //显示意图 public void enter(View view) { Intent intent = new Intent();//创建一个空的意图 intent.setClassName(ge ...

  5. Android 显示意图和隐式意图的区别

    意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么.        意图的作用:        1.激活组件   ...

  6. [android] 隐式意图激活另外一个activity

    随着api的升级,系统的很多应用包名和类名都改掉了,所以很多时候,打开系统应用的时候会报错,隐式意图就是解决组件之间松耦合,描述动作行为 获取Intent对象,通过new出来 调用Intent对象的s ...

  7. Android的显示意图和隐式意图总结

    显示意图 简而言之: 通过指定特定Activity的包名和类名开启Activity 应用场景: 一般应用于本App内的activity间的跳转. XML配置信息: AndroidManifest.xm ...

  8. android intent 隐式意图和显示意图(activity跳转)

    android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity  B ...

  9. Android学习记录(7)—Intent中显示意图和隐式意图的用法

    Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 ...

随机推荐

  1. IE9父容器overflow:auto时,子容器状态更改导致滚动条下出现额外空间的问题探讨

    IE的每次跟新都会有一些奇葩的bug,我们默默承受了. 这个问题在项目中出现困扰了我近一个星期,这里记录一下.看下面实例 <style> .panel{ width: 200px; ove ...

  2. c# 游戏策划配置工具

    该工具是提供策划配置excel数据,导出到mysql数据库,以及生成xml文件,和对应的xml解析实体类 实现了程序 excel 列名 ID =P 表示ID这列是唯一字段 =S=300 表示这列类型是 ...

  3. 微信小程序官方文档错误整理

    大致看了一遍微信小程序文档,发现有几处微小的错误,但瑕不掩瑜.记录下,以后发现了还会继续在此添加.如果有记录不对的,请及时指出错误. 1.视图层->WXSS->尺寸单位 明显错误,应该为 ...

  4. 在CentOS上部署Asp.Net MVC3的第一次尝试

    关注mono已经很久了,现在才有时间真正的尝试一下在linux中部署asp.net的网站,也算是记录 一下自己的第一次尝试吧. 我的实践的环境是win7 + VM10 + CentOS6.5 下面就是 ...

  5. C#删除程序自身【总结】

    偶然看到一个可以自删除的程序,于是了解下如何实现.然后整理如下: 思路: 在.NET程序中,因为运行中的程序是受系统保护的,不能自己删除自身的,所以自删除的思路:  在关闭本程序之前启动新的进程打开另 ...

  6. java一点小的知识点

    1.在dos底下编译运行java文件时,若有包名,需要建立包名对应的文件夹,然后编译的时候写出包名:java.mypack.PackageDemo 2.代码中有包名的时候,自动创建文件夹并编译的方法: ...

  7. java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX

    出现这个问题的解决方案就是将原有的jar删除  然后重新下载过一遍就可以使用了  我估计是元数据等损坏了

  8. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  9. 点我吧工作总结(技术篇) zookeeper

    我思故我在,提问启迪思考! 1.什么是zookeeper? 2.zookeeper与dubbo.springMVC之间的协同工作? http://doc.okbase.net/congcong68/a ...

  10. Scalaz(0) - 写在前面

    面向对象编程范畴(OOP)从80年代C++到90年代java的兴起已经经历了几十年的高潮,是不是已经发展到了尽头,该是函数式编程(FP)开始兴旺发达的时候了吧.这样说似乎心眼儿有点坏,可能会得罪当今大 ...