标签:
新建项目, 新建一个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>
最终界面如下:
点击按钮后跳转到第二个页面
对于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); } } }
标签:
原文地址:http://www.cnblogs.com/wuyudong/p/5658935.html