服务不会开始

时间:2013-03-25 05:27:39

标签: android-activity android-service android-broadcast

我正在尝试启动一个只要导航活动正在运行就会在后台运行的服务。问题是该服务似乎没有启动。应用程序没有崩溃,它试图尽我所能启动服务,但似乎没有任何事情发生。有人能告诉我我做错了什么吗?此外,我正在尝试使用本地广播公司将数据字符串从服务发送到调用活动,我是否正确实现了它?最后,我似乎无法让Navigation活动执行run(),这样做的最佳方法是什么,所以run()被重复调用,直到活动关闭?这是我第一次尝试制作一个Android应用程序,所以用新手来解释:)谢谢

FYI MainActivity启动导航活动,启动Service AugiActivity

MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void call_ioio(View v)
{   
    Intent intent = new Intent(this, AugiActivity.class);
    startActivity(intent);
}

public void navigation(View v)
{   
    System.out.println("Got to Mainactivity start Navigation");
    Intent intent = new Intent(this, Navigation.class);
    startActivity(intent);
}

}

导航活动:

public class Navigation extends MapActivity implements Runnable, 
SurfaceHolder.Callback{

String LED = null;
String MAG = null;
String BAR = null;
String GYRO = null;
String EULER = null;    
String GGA = null;

private BroadcastReceiver MessageReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        LED = intent.getStringExtra("LED");
        MAG = intent.getStringExtra("MAG");
        BAR = intent.getStringExtra("BAR");
        GYRO = intent.getStringExtra("GYRO");
        EULER = intent.getStringExtra("EULER");
        GGA = intent.getStringExtra("GGA"); 
        run();
    }
};

boolean previewing = false;
boolean map_view = true;
boolean cam_view = false;
boolean sen_view = false;

LinearLayout SS;
SurfaceView CC;
MapView MM;

TextView light_voltage_TV;
TextView bar_data_TV;
TextView mag_TV;
TextView gyro_TV;
TextView gyro_rate_TV;
TextView acel_TV;
TextView acel_euler_TV;
TextView gga_TV;
TextView gsa_TV;
TextView gsv_TV;
TextView rmc_TV;
TextView vtg_TV;

// CAMERA VARIABLES
    Camera cam;
    SurfaceView cam_TV;
    SurfaceHolder holder;       

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    MapView mapview = (MapView) findViewById(R.id.mapview);
    mapview.setBuiltInZoomControls(true);   

    getWindow().setFormat(PixelFormat.UNKNOWN);

    cam_TV = (SurfaceView) findViewById(R.id.CAMERA_View);
    holder = cam_TV.getHolder();
    holder.addCallback((Callback) this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    LocalBroadcastManager.getInstance(this).registerReceiver(MessageReceiver, 
            new IntentFilter("IOIOData"));

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            // Lots of linking view variables to xml views here

    System.out.println("Got to Navigation OnCreate");
    startService(new Intent(this, AugiActivity.class));     
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_navigation, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void sensorLayer(View v)
{
    // set view visable/invisible
}

public void mapLayer(View v)
{
    //set view visable/invisible
}

public void cameraLayer(View v)
{
    //set view visable/invisible
}   

@Override
public void run() {
    System.out.println("Got to Navigation RUN");

    //update text views with string data
}
}

AugiActivity:

public class AugiActivity extends Service { 

public Activity activity_name;

class IOIO extends IOIOActivity {   


// Various unrelated global variables

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    activity_name = this.getParent();
    System.out.println("Got to AugiActivity OnCreate");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.        
    return true;
}

class Looper extends BaseIOIOLooper {

    public Activity activity_name;

    Looper(Activity x)
    {
        this.activity_name = x;
    }       

    // Communication variables

    /**
    * Called every time a connection with IOIO has been established.
    * Typically used to open pins.
    *   
    * @throws ConnectionLostException
    *             When IOIO connection is lost.
     * @throws InterruptedException 
    * 
    * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
    */
        @Override
        protected void setup() throws ConnectionLostException, 
InterruptedException 
        {
            // OPEN LIGHT SENSOR PINS


            // ESTABLISH I2C CONNECTION


            // ESTABLISH UART CONNECTION                

            bar_calibration();      
            mag_setup();
            imu_setup();    
            try {
                gps_setup();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // BAROMETER CALIBRATION METHOD
        private void bar_calibration()
        {
            // do setup
        }

        // MAGNETOETER SETUP METHOD
        private void mag_setup()
        {
            // do setup
        }

        // IMU SETUP METHOD
        private void imu_setup()
        {
            // do setup
        }

        private void gps_setup() throws IOException, InterruptedException
        {               
            // do setup     
        }

    /**
    * Called repetitively while the IOIO is connected.
    * 
    * @throws ConnectionLostException
    *             When IOIO connection is lost.
     * @throws InterruptedException 
    * 
    * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
    */


    @Override
        public void loop() throws ConnectionLostException, 
InterruptedException 
        {           
            get_light();

            get_temp();

            get_pressure();

            get_north();

            get_imu();

            System.out.println("Got to AugiActivity LOOP");
            try {
                get_gps();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            led_.write(true);   

            sendMessage();
        }

        private void get_light()
        {
            // get data
        }

        private void get_temp()
        {
            // get data
        }

        private void get_pressure()
        {
            // get data
        }

        private void get_north()
        {
            // get data
        }

        private void get_imu()
        {
            // get data
        }

        private void get_gps() throws IOException, InterruptedException
        {   
            // get data 
            }

        private void sendMessage()
        {
            Intent intent = new Intent("IOIOData");     

            intent.putExtra("LED", get_LED());

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);

            intent.putExtra("MAG", get_MAG());

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);

            intent.putExtra("BAR", get_BAR());

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);

            intent.putExtra("GYRO", get_GYRO());

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);

            intent.putExtra("EULER", get_EULER());

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);

            intent.putExtra("GGA", gps_GGA);

LocalBroadcastManager.getInstance(activity_name).sendBroadcast(intent);
        }
    }
    /**
    * A method to create our IOIO thread.
    * 
    * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
    */
    @Override
    protected IOIOLooper createIOIOLooper() {
        return new Looper(activity_name);
    }       

    public String get_LED()
    {
        // code here            
    }

    public String get_MAG()
    {
        // code here
    }

    public String get_BAR()
    {
        // code here            
    }

    public String get_GYRO()
    {
        // code here            
    }

    public String get_EULER()
    {
        // code here            
    }

    public String get_GPS()
    {
        // code here            
    }

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return this.onBind(intent);
}   
}

1 个答案:

答案 0 :(得分:0)

我不是专家,但在这里你有一项服务活动。 您使用startService()Intent致电AugiActivity,这是一项服务(很难用此类名称跟踪您)。

我看到的问题是您甚至没有实施服务的onCreate()onStartCommand()onDestroy()方法。在这种方法中,您可以管理此处嵌套对象的生命周期。

无论如何,我认为服务中的活动并不是很干净。你的目的到底是什么?为什么你有嵌套活动?你似乎没有任何布局(也许我错过了,但是有很多代码!:D),那么为什么不停留在服务本身,而不是在嵌套类中执行所有操作?

如果您希望拥有与后台服务通信的用户界面,则可以使用绑定到服务的活动(但创建2个不同的类:一个用于服务,一个用于具有接口的活动)。请参阅Android开发者指南中的bound services