android中可以使用Intent时 可以用setClass() 还可以使用ComponentName

Java 代码

package com.huawei;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;public class Test extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//组件名称,第一个参数是包名,也是主配置文件Manifest里设置好的包名第二个是类名,要带上包名ComponentName com = new ComponentName("com.huawei", "com.huawei.Test1");Intent  intent = new Intent();//设置部件intent.setComponent(com);    Test.this.startActivity(intent);}});    }}


经查看源码发现 Intent 的setClass() 方法的实现正是使用ComponentName 类:

public Intent setClass(Context packageContext,Class<?> cls){   mComponent=new ComponentName(packageContext,cls);return this;}

Intent 的 构造方法也是如此:

public Intent(Context packageContext,Class<?> cls){      mComponent=new ComponentName(packageContext,cls);}

上面例子中的 setComponent()方法:

public Intent setComponent(ComponentName component){      mComponent =component;return this;}

以上可以看出无论那种方法调用的都是相似的代码;

参考:http://hellorheaven.iteye.com/blog/860193点击打开链接

更多相关文章

  1. Android 代码片段
  2. Android中的多种文件读写操作方法
  3. android 监听其它app的错误或者崩溃的方法 (ProcessErrorStateInf
  4. 第一代Android壳源码--某某公司早期壳代码加固原理分析
  5. Android中获取屏幕长宽的方法
  6. Android方法的传递值及其改变
  7. 【Android】打开/读取文件的方法
  8. Android 滑动效果代码分享

随机推荐

  1. Android(安卓)- Rerofit-RxJava(转载)
  2. Android(安卓)上传图片到服务器(多文件上
  3. Android中彩信文件的读取
  4. android 权限介绍(一)
  5. android Toolbar的使用结合状态栏与返回
  6. Android(安卓)音乐乱码问题修改
  7. Activity属性设置大全
  8. Delphi Android(安卓)USB Interface with
  9. Android(安卓)recyclerview更新item位置
  10. Android音频系统之四AudioPolicy