本章博客,记录的是跨应用启动Service。
我们需要创建一个应用service1,其中包含一个MyService,部分代码如下:

    @Override    public void onCreate() {        // TODO Auto-generated method stub        System.out.println("onCreate");        super.onCreate();    }    @Override    public void onDestroy() {        System.out.println("onDestroy");        super.onDestroy();    }

之前的博客有介绍过创建Service,可参考创建: http://blog.csdn.net/gaopeng0071/article/details/45153495

然后再创建一个service2应用。activity的代码如下,
我们注意21行的代码,ComponentName对象中,第一个参数是要跳转到应用的service包名,第二个参数是Service类的全路径。

package com.example.service2;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity implements OnClickListener {    Intent serviceIntent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        serviceIntent = new Intent();        serviceIntent.setComponent(new ComponentName("com.example.service1", "com.example.service1.MyService"));        findViewById(R.id.button1).setOnClickListener(this);        findViewById(R.id.button2).setOnClickListener(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onClick(View v) {        switch(v.getId()){        case R.id.button1:            startService(serviceIntent);            break;        case R.id.button2:            stopService(serviceIntent);            break;        }    }}

通过21行代码的跳转即可,实现跨应用启动Service。

更多相关文章

  1. Android上的Ubuntu Debian Armel
  2. Android(安卓)Bitmap用法总结
  3. Android(安卓)主动 try-catch 主线程的 Exception
  4. Android(安卓)活动的最佳实践
  5. android context调用startActivity时报错:Calling startActivity(
  6. Appium自动化测试-Android启动实例(2)
  7. Android(安卓)JNI和NDK学习(2)--编程入门
  8. Android(安卓)调用系统相机部分源码分析
  9. Android判断是Wifi还是4G网络代码

随机推荐

  1. Android中实现应用切换主题机制
  2. Android客制化------开机拷贝文件到内置
  3. Android(安卓)SQLite 开发教程(2): andro
  4. Android(安卓)学习笔记(3)—— ImageView/R
  5. 免费短信和电话服务TextFree在Android上
  6. Android模拟键盘之菜单键消失
  7. Android学习系列(14)--App调试的几个命令
  8. Android(安卓)EventBus库的使用
  9. android中相对布局入门
  10. ok6410 eclipse在线调试android程序