android组件通讯 Intent-ComponnentName属性

简介:

ComponnentName属性应用实例

/Chapter06_Intent_ComponentName/src/com/amaker/ch06/app/MainActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.ch06.app;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.  
  13. /**  
  14.  * 测试Intent的ComponentName属性  
  15.  */ 
  16. public class MainActivity extends Activity {  
  17.     private Button btn;  
  18.     @Override 
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         // 设置视图布局  
  22.         setContentView(R.layout.main);  
  23.         // 实例化Button  
  24.         btn = (Button)findViewById(R.id.myButton01);  
  25.         // 添加单击监听器  
  26.         btn.setOnClickListener(new OnClickListener() {  
  27.             @Override 
  28.             public void onClick(View v) {  
  29.                 // 实例化组件名称  
  30.                 ComponentName cn = new ComponentName(MainActivity.this"com.amaker.ch06.app1.MyActivity");  
  31.                 // 实例化Intent  
  32.                 Intent intent = new Intent();  
  33.                 // 为Intent设置组件名称属性  
  34.                 intent.setComponent(cn);  
  35.                 // 启动Activity  
  36.                 startActivity(intent);  
  37.             }  
  38.         });  
  39.     }  

/Chapter06_Intent_ComponentName/src/com/amaker/ch06/app1/MyActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.ch06.app1;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.widget.TextView;  
  10.  
  11. import com.amaker.ch06.app.R;  
  12.  
  13. /**  
  14.  * 测试Intent的ComponentName属性  
  15.  */ 
  16. public class MyActivity extends Activity {  
  17.     // 声明TextView  
  18.     private TextView tv;  
  19.     @Override 
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         // 设置视图布局  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.my_layout);  
  24.         // 获得Intent  
  25.         Intent intent = this.getIntent();  
  26.         // 获得组件名称对象  
  27.         ComponentName cn = intent.getComponent();  
  28.         // 获得包名称  
  29.         String packageName = cn.getPackageName();  
  30.         // 获得类名称  
  31.         String className = cn.getClassName();  
  32.         // 实例化TextView  
  33.         tv = (TextView)findViewById(R.id.TextView01);  
  34.         // 显示  
  35.         tv.setText("组件包名称:"+packageName+"\n"+"组件类名称:"+className);  
  36.     }  

/Chapter06_Intent_ComponentName/res/layout/main.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9.     <Button   
  10.         android:text="测试Intent的组件名称属性"   
  11.         android:id="@+id/myButton01"   
  12.         android:layout_width="wrap_content"   
  13.         android:layout_height="wrap_content"></Button> 
  14. </LinearLayout> 

/Chapter06_Intent_ComponentName/res/layout/my_layout.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9.  
  10.     <TextView   
  11.         android:text="@+id/TextView01"   
  12.         android:id="@+id/TextView01"   
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content"></TextView> 
  15.       
  16. </LinearLayout> 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080676

相关文章
|
2月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
33 0
|
3月前
|
Android开发 开发者
什么是Android Jetpack,它包括哪些组件?
什么是Android Jetpack,它包括哪些组件?
39 0
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
4月前
|
XML Java Android开发
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
72 0
|
2月前
|
数据可视化 Android开发
[Android 四大组件] --- Service
[Android 四大组件] --- Service
24 0
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
2月前
|
存储 数据库 Android开发
安卓四大组件是什么?
安卓四大组件是什么?
|
3月前
|
数据库 Android开发 开发者
Android基础知识:什么是Android应用的四大组件?
Android基础知识:什么是Android应用的四大组件?
59 1
|
4月前
|
传感器 监控 物联网
Android Ble蓝牙App(三)特性和属性
Android Ble蓝牙App(三)特性和属性
|
4月前
|
XML 安全 Java
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
Android Studio App开发之广播组件Broadcast的讲解及实战(包括收发标准、有序、静态广播实现手机震动功能 附源码)
36 0