ComponentName 启动第三方app的Activity或

2018-06-30  本文已影响0人  feifei_fly

ComponentName

顾名思义,就是组件名称,通过调用intent.setComponent()方法,我们可以打开另一个应用的Activity或者Service

创建ComponentName 需要两个参数:

(1)第三方应用的包名

(2)要打开的Activity和Service的全名称(包名+类名)

public ComponentName(@NonNull String pkg, @NonNull String cls) {
        if (pkg == null) throw new NullPointerException("package name is null");
        if (cls == null) throw new NullPointerException("class name is null");
        mPackage = pkg;
        mClass = cls;
    }
ComponentName chatActivity =new ComponentName("com.feifei.example", "com.feifei.example.ChatActivity");

    Intent intent =new Intent();

    intent.setComponent(chatActivity);

    startActivity(intent);

ComponentName chatService =new ComponentName("com.feifei.example", "com.feifei.example.ChatService");

    Intent intent =new Intent();

    intent.setComponent(chatService );

    startService(intent);

通过Component启动 系统应用
Intent i = new Intent();

ComponentName comp = new ComponentName("com.android.camera","com.android.camera.GalleryPicker");

i.setComponent(comp);

i.setAction("android.intent.action.VIEW");

startActivity(i);
Intent mIntent = new Intent();

ComponentName comp = new ComponentName("com.android.camera","com.android.camera.Camera");

mIntent.setComponent(comp);

mIntent.setAction("android.intent.action.VIEW");

startActivity(mIntent);
注意:
 Intent intent = new Intent();
        intent.setClassName("org.cvpcs.android.sensordump","org.cvpcs.android.sensordump.AListSensors");
        startActivity(intent);

上一篇 下一篇

猜你喜欢

热点阅读