博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跨应用启动service
阅读量:7089 次
发布时间:2019-06-28

本文共 1805 字,大约阅读时间需要 6 分钟。

package com.example.yabushan.aidsservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;//创建一个服务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 on start");    }    @Override    public void onDestroy() {        super.onDestroy();        System.out.println("service deastroy");    }}

  

package com.example.yabushan.anotherapp;import android.content.ComponentName;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;//另一个apppublic 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(); //传入上边App的包名以及service的类名        serviceIntent.setComponent(new ComponentName("com.example.yabushan.aidsservice","com.example.yabushan.aidsservice.AppService"));        findViewById(R.id.startService).setOnClickListener(this);        findViewById(R.id.stopService).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.startService:                startService(serviceIntent);                break;            case R.id.stopService:                stopService(serviceIntent);                break;        }    }}

  

转载于:https://www.cnblogs.com/yabushan/p/4990111.html

你可能感兴趣的文章
PHP/Javascript 数组定义 及JSON中的使用 ---OK
查看>>
php中urldecode()和urlencode()起什么作用啊
查看>>
UVA 11542 Square 高斯消元 异或方程组求解
查看>>
Nginx的内部(进程)模型
查看>>
基于设备树的controller学习(1)
查看>>
递归--练习1--noi3089爬楼梯
查看>>
慢慢过渡到个人博客
查看>>
【转】spring boot web相关配置
查看>>
oc53--autorelease注意事项
查看>>
sigmod2017.org
查看>>
MongoDB集群运维笔记
查看>>
Python代码优化及技巧笔记(一)
查看>>
Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
查看>>
Ocelot 集成Butterfly 实现分布式跟踪
查看>>
(转)各种语言写网络爬虫有什么优点缺点
查看>>
好用的端口监控软件:Port Explorer
查看>>
Cisco无线控制器配置Radius
查看>>
iota 币产生私钥的方法
查看>>
Mysql数据类型DECIMAL(M,D)用法
查看>>
006-Shell printf 命令
查看>>