美文网首页
跨应用使用Service

跨应用使用Service

作者: csp | 来源:发表于2017-03-03 16:55 被阅读57次

从AnotherApp启动App的Service:
首先,在App中创建AppService:
public class AppService extends Service {
public AppService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();
    System.out.println("Service start");
}

@Override
public void onDestroy() {
    super.onDestroy();
    System.out.println("service destroyed");
}

}
之后在AnotherApp里面启动App里面的AppService:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Intent serviceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    serviceIntent = new Intent();
    serviceIntent.setComponent(new ComponentName("com.chenshipeng.startservicefromanotherapp","com.chenshipeng.startservicefromanotherapp.AppService"));

    findViewById(R.id.btnStartAppService).setOnClickListener(this);
    findViewById(R.id.btnStopAppService).setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnStartAppService:
            startService(serviceIntent);
            break;
        case R.id.btnStopAppService:
            stopService(serviceIntent);
            break;
    }
}

}
这里的Intent通过设置组件名字,使用包名和服务的名字,来创建一个显示的Intent,这样就可以用来启动另外一个App的服务了。

相关文章

  • 跨应用使用Service

    从AnotherApp启动App的Service:首先,在App中创建AppService:public clas...

  • 跨应用绑定Service

    Android使用AIDL(Android接口定义语言)来进行应用之间通信。首先需要新建一个AIDL文件,在App...

  • Service

    总是使用显式Intent启动或者绑定Service,且不要为Service声明IntentFilter,保证应用安...

  • 跨应用绑定Service并通信

    在AIDL文件里面添加方法:void setData(String data);使用的时候需要实现这个接口,pub...

  • Android性能优化

    Android性能优化 合理管理内存 节制的使用Service 如果应用程序需要使用Service来执行后台任务的...

  • Part1_Android性能优化

    Android性能优化 合理管理内存 节制的使用Service 如果应用程序需要使用Service来执行后台任务的...

  • 补全计划| Android性能优化分析

    Android性能优化 合理管理内存 节制的使用Service 如果应用程序需要使用Service来执行后台任务的...

  • WebService的简单例子

    webservice就是应用程序之间跨语言的调用 1、什么是webservice Web service是一个平台...

  • Android内存优化汇总

    1.使用保守的Service实现模块内具体功能时,尽量避免以应用内常驻后台的Service方式实现。如果应用需要使...

  • Kubernetes Deployment 的故障排查可视化指南

    将应用部署到 Kubernetes 时通常会使用 Deployment、Service、Ingress,整个应用从...

网友评论

      本文标题:跨应用使用Service

      本文链接:https://www.haomeiwen.com/subject/nfdrgttx.html