启动第三方的Activity,service

来源:互联网 发布:虫虫钢琴软件 编辑:程序博客网 时间:2024/06/07 19:39

这两天一个新来的同事,我让他帮我写监控相关的功能,因为我让他把监控这个单独做成一个app, 里面涉及到自动登录,还有后台启动等相关的功能,

写了两周 ,功能做完了,但是代码质量不是很满意 ,里面涉及到Launcher中去启动监控app里面相关的界面,以及service,他是用广播来实现的,代码laucnher和监控app交互比较多,里面全是广播,我看了一下,感觉以后维护的同事会很难过 ,就整改了一下 ,所有的逻辑都写在监控app里面 ,Launcher仅仅是启动app里面的service就好 ,这样维护起来就简单多了

刚好就总结一下app启动第三方的Activity 和service , 网上的资源还是比较多的,这里做一个复合类的总结

app启动第三方的Activity的方法比较多


1:直接通过报名去启动Activity 

    Intent intent= getPackageManager().getLaunchIntentForPackage("com.reeman.demo");
    startActivity(intent);

 

2:通过清单文件定义的Action

  Intent intent = new Intent();
  intent.setAction("com.reeman.demo");
  startActivity(intent);
 
<intent-filter>
      <action android:name="com.reeman.demo" />
       <category android:name="android.intent.category.DEFAULT" />               
 </intent-filter>


3:通过Scheme

    Intent intent = new Intent();
    intent.setData(Uri.parse("joyodream://......"));
    startActivity(intent);
 
 <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="joyodream" android:ssp="jiji" />               
  </intent-filter>


4:通过包名类名来打开Activty

   ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.plugin.sns.ui.SnsTimeLineUI");

intent.addCategory(Intent.CATEGORY_LAUNCHER);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setComponent(cmp);startActivity(intent);

5:打开第三方的应用程序的Service

     在自己的程序中启动

     

    Intent intent = new Intent();        intent.setAction("com.mirror.mobile.action.START_SERVICE_MONITOR");  //应用在清淡文件中注册的action        intent.setPackage("com.mirror.mobile");                  //应用程序的包名        startService(intent); 

    第三方的service清单文件注册

       

 <service            android:name="com.reeman.two.TestService"            android:enabled="true"            android:exported="true"            android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.START_PCM_PLAY_SERVICE" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </service>

其他的逻辑都是一样的 ,