ComponnentName属性应用实例
/Chapter06_Intent_ComponentName/src/com/amaker/ch06/app/MainActivity.java
代码
package com.amaker.ch06.app;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 测试Intent的ComponentName属性
*/
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置视图布局
setContentView(R.layout.main);
// 实例化Button
btn = (Button)findViewById(R.id.myButton01);
// 添加单击监听器
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 实例化组件名称
ComponentName cn = new ComponentName(MainActivity.this, "com.amaker.ch06.app1.MyActivity");
// 实例化Intent
Intent intent = new Intent();
// 为Intent设置组件名称属性
intent.setComponent(cn);
// 启动Activity
startActivity(intent);
}
});
}
}
/Chapter06_Intent_ComponentName/src/com/amaker/ch06/app1/MyActivity.java
代码
package com.amaker.ch06.app1;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.amaker.ch06.app.R;
/**
* 测试Intent的ComponentName属性
*/
public class MyActivity extends Activity {
// 声明TextView
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
// 设置视图布局
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
// 获得Intent
Intent intent = this.getIntent();
// 获得组件名称对象
ComponentName cn = intent.getComponent();
// 获得包名称
String packageName = cn.getPackageName();
// 获得类名称
String className = cn.getClassName();
// 实例化TextView
tv = (TextView)findViewById(R.id.TextView01);
// 显示
tv.setText("组件包名称:"+packageName+"\n"+"组件类名称:"+className);
}
}
/Chapter06_Intent_ComponentName/res/layout/main.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="测试Intent的组件名称属性"
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
/Chapter06_Intent_ComponentName/res/layout/my_layout.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>