您的位置:首页 > 移动开发 > Android开发

android跨进程启动service

2016-02-22 17:09 459 查看
1,启动服务的客户端
MyLog.i(TAG,"启动服务");
String shopId = PreferencesUtil.getString(MainActivity.this, Constants.SHOPID_KEY);
String userName = PreferencesUtil.getString(MainActivity.this, Constants.USERNAME_KEY);
String pwd = PreferencesUtil.getString(MainActivity.this, Constants.USERPWD_KEY);

String packageName = "com.sonar.android.servicedemo.service";//需要开启服务的app包名
String serviceClassName = packageName + "SonarService";//服务的类名全限定名
Intent serviceIntent = new Intent();
serviceIntent.putExtra("shopId", shopId);
serviceIntent.putExtra("userName", userName);serviceIntent.putExtra("password", pwd);serviceIntent.setComponent(new ComponentName(packageName, serviceClassName));startService(serviceIntent);//启动服务
2,被启动的服务
package com.sonar.android.servicedemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
* Created by Administrator on 2016/2/22.
*/
public class SonarService extends Service {
final  String TAG = SonarService.class.getSimpleName();
@Override
public void onCreate() {
Log.i(TAG,"服务启动");
super.onCreate();
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"shopId="+intent.getStringExtra("shopId"));
return super.onStartCommand(intent, flags, startId);
}
}
AndroidManifest.xml
<service android:name=".service.SonarService"android:exported="true"android:icon="@drawable/normal7"/>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: