Android -- 跨应用启动Service

本章博客,记录的是跨应用启动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。

时间: 2024-07-28 22:40:08

Android -- 跨应用启动Service的相关文章

Android -- 跨应用绑定service(AIDL)

之前一章记录了跨应用启动service,之前学习过startService于bindService的区别及用法. 使用bindService方法,activity需要service返回一个Binder对象,那么如果是两个应用怎么实现呢,这里就涉及到了一个AIDL的概念. AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication,

Android 跨进程启动Activity黑屏(白屏)的三种解决方案

原文链接:http://www.cnblogs.com/feidu/p/8057012.html 当Android跨进程启动Activity时,过程界面很黑屏(白屏)短暂时间(几百毫秒?).当然从桌面Lunacher启动一个App时也会出现相同情况,那是因为App冷启动也属于跨进程启动Activity.为什么没会出现这种情况呢?真正元凶就是Android创建进程需要准备很多资源,它是一个耗时的操作. 黑屏(白屏)原因 当A进程启动B进程中的一个Activity时,Android系统会先有zygo

Android开发之AIDL的使用一--跨应用启动Service

启动其他App的服务,跨进程启动服务. 与启动本应用的Service一样,使用startService(intent)方法 不同的是intent需要携带的内容不同,需要使用intent的setComponent()方法. setComponent()方法需要传入两个参数,第一个参数是包名,第二个参数是组件名.即,第一个参数传入要启动的其他app的包名,第二个参数传入的时候要启动的其他app的service名. 看下面的例子:(aidlserviceapp应用通过button启动aidlservi

Android跨应用启动

前绪: 相信大家,很多时候都是在自己的应用中,启动自己写的Activity,Service.BroadcastReceiver.contentProvider .换句话说,这些都只是 * 单个应用中 组件间 * 的启动.而我们下面要谈论的是 两个应用间 组件 的启动.即--使用 隐式Intent方式 启动应用B的某个组件. 一.在开始之前,先来梳理一下跨应用启动的2种方式: 第一种:在Activity中,启动另一个app的组件. 第二种:在Service中,启动另一个app的组件. 从所周知,A

Android -- 跨应用绑定service并通信

之前记录过跨应用绑定service:http://blog.csdn.net/gaopeng0071/article/details/46049929,那么绑定后如何进行通信呢,下面我们就来学习下. 第一步, 需要修改service1项目中aidl,增加一个方法. package com.example.service1.aidl; interface IMyService { void basicType(); void setName(String name); } setName用于存储n

【XFeng安卓开发笔记】四大基本组件——跨应用启动service

APP MainActivity.java package com.xfeng.startservicefromanotherapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected vo

Android 两种启动Service(远程)的方式:Bind 与Start

前言:本文主要讨论启动远程Service. Service和Activity不在一个工程里面,也即不在一个App里面.不在一个进程里,所以会用到AIDL. Service的android:process属性未指定. 一.startService 1.通过调用startService启动服务的过程: onCreate ->onStartCommand ->onStart startService 仅用于启动服务,如果Activity需要与Service进行通信,需利用Broadcast. 2.而

跨应用启动Service

1 package com.example.metrox.l16; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 7 public class AppService extends Service { 8 9 10 public AppService() { 11 } 12 13 @Override 14 public IBinder onBind(I

跨应用启动Service并传递数据

启动: 在Android5.0之前可以通过隐式intent启动服务,但是Android5.0之后不可以了. 在两个Application之间是不可能获取到Service的定义的,需要调用SetComponent函数: serviceIntent = new Intent(); serviceIntent.setComponent(new ComponentName("com.wanxiang.www.startservicefromanotherapp", "com.wanx