主界面
package cn.yza.other;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class OtherActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Uri uri = Uri
// .parse("content://com.yza.provider.personprovider/person/");
// this.getContentResolver().registerContentObserver(uri, true,
// new PersonContentObserver(new Handler()));
}
public void openNewWin(View v) {
Intent intent = new Intent();
//方法1
//intent.setClass(this, TempActivity.class);//要激活的组件
//方法2 反射
// intent.setClassName(this, "cn.yza.other.TempActivity");
//方法3
//intent.setComponent(new ComponentName(this, TempActivity.class));
//方法4
//Intent intent = new Intent(this, TempActivity.class);
//方法5 用于激活其他应用
//单个添加
intent.setClassName("cn.yza.other", "cn.yza.other.TempActivity");
// intent.putExtra("company", "yza");
// intent.putExtra("age", 34);
//批量添加
Bundle b = new Bundle();
b.putString("company", "yza");
b.putInt("age", 12);
intent.putExtras(b);
// startActivity(intent);
startActivityForResult(intent, 200);//添加请求码 控制返回时候的路径
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getStringExtra("result");
Toast.makeText(this, result, 1).show();
}
}
package cn.yza.other;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class TempActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
Intent intent = getIntent();
//取值方法1
// String com = intent.getStringExtra("company");
// int age = intent.getIntExtra("age", 0);
//取值方法2
Bundle b = intent.getExtras();
String com =b.getString("company");
int age =b.getInt("age");
TextView tv = (TextView) this.findViewById(R.id.info);
tv.setText("com" +com+" age" + age);
}
public void closeActivity(View v){
Intent intent = new Intent();
intent.putExtra("result", "张晓晓");
setResult(30, intent);
this.finish();//关闭当前
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:text="新的窗口"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="openNewWin"/>
</LinearLayout>
temp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是新的activity"
android:id="@+id/info" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭"
android:onClick="closeActivity"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.yza.other"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".OtherActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="new窗口"
android:name=".TempActivity"></activity>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:label="test my app"
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.yza.other" />
</manifest>