Android Studio开发基础之启动Service,并通过从Activity向Service传递数据

       本实例演示启动Service,并通过从Activity向Service传递数据,新建一个Service,并敲如下代码:

package com.example.lhb.startservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.ViewDebug;
import android.widget.Toast;

public class MyService extends Service {
    private boolean Running=false;
    private String data="默认信息!!!";
    public MyService() {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
        data=intent.getStringExtra("data");//这里的intent是参数里的,不是自定义的
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Running=true;
     

你可能感兴趣的:(《GIS程序设计》)