首先定义一个主布局文件
hello_activity.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:id="@+id/cust_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn" android:textColor="@android:color/white" android:textSize="28sp" /> <!-- 中间动态加载View --> <ScrollView android:id="@+id/containerBody" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:measureAllChildren="true" > </ScrollView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@android:color/black" android:orientation="horizontal" > <!-- 功能模块按钮1 --> <Button android:text="@string/btn" android:id="@+id/btnModule1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginLeft="7dp" android:layout_marginTop="3dp" /> <!-- 功能模块按钮2 --> <Button android:text="@string/btn" android:id="@+id/btnModule2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginLeft="7dp" android:layout_marginTop="3dp" /> <!-- 功能模块按钮3 --> <Button android:text="@string/btn" android:id="@+id/btnModule3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginLeft="7dp" android:layout_marginTop="3dp" /> </LinearLayout> </LinearLayout>
HelloActivity.java
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.helloactivity;
import android.view.View;
import android.content.ComponentName;
import android.content.Intent;
import android.widget.Button;
import android.widget.ScrollView;
//import android.media.MediaScanner; @hide
import android.media.MediaPlayer;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.SystemClock;
import android.graphics.Paint;
import android.os.Handler;
import android.widget.Toast;
import android.util.Log;
public class HelloActivity extends ActivityGroup {
//am start -n com.cmcc.mbbms/com.cmcc.mbbms.MainActivity
private ScrollView container = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置视图
setContentView(R.layout.hello_activity);
container = (ScrollView) findViewById(R.id.containerBody);
// 模块1
Button btnModule1 = (Button) findViewById(R.id.btnModule1);
btnModule1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = new Intent();
ComponentName comp = new ComponentName(
"com.cmcc.mbbms",
"com.cmcc.mbbms.MainActivity");
intent.setComponent(comp);
container.addView(getLocalActivityManager().startActivity(
"Module1",
intent)
.getDecorView());
}
});
// 模块2
Button btnModule2 = (Button) findViewById(R.id.btnModule2);
btnModule2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = new Intent();
ComponentName comp = new ComponentName(
"com.android.calendar",
"com.android.calendar.MonthActivity");
intent.setComponent(comp);
container.addView(getLocalActivityManager().startActivity(
"Module2",
intent)
.getDecorView());
}
});
// 模块3
Button btnModule3 = (Button) findViewById(R.id.btnModule3);
btnModule3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = new Intent();
ComponentName comp = new ComponentName(
"com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
intent.setComponent(comp);
container.addView(getLocalActivityManager().startActivity(
"Module3",
intent)
.getDecorView());
}
});
}
}
ActivityGroup 为什么不能启动一个其他进程的Activity
解决方法:
1、在activityThread.java的代码里添加对安全忽略即可。
r.packageInfo = getPackageInfo(aInfo.applicationInfo,
Context.CONTEXT_INCLUDE_CODE + Context.CONTEXT_IGNORE_SECURITY);
2、 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:sharedUserId="com.android.activitygrouptest"
android:versionName="1.0">
设定sharedUserId一致就可以了,这样两个apk会加载到同一个process中运行,同时要把你要启动的activity launchemode 设定为singletop。
只能启动同一个进程的Actvity Intent intent = new Intent(HelloActivity.this,HelloActivity1.class);
public class HelloActivity2 extends ActivityGroup {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//动态添加按钮
LinearLayout tp = new LinearLayout(this);
LayoutParams ltp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setWidth(100);
btn.setText("HelloActivity2");
tp.addView(btn,ltp);
this.setContentView(tp);
}
}