0

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

FYI MainActivity 启动 Navigation Activity,它启动 Service AugiActivity

主要活动:

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
}
}

活动:

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);
}   
}
4

1 回答 1

0

我不是专家,但在这里你有一个服务中的活动。你打电话startService()Intent时候AugiActivity是一个服务(很难用这种名字来关注你)。

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

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

如果您想要一个与后台服务通信的用户界面,那么您可以使用绑定到服务的活动(但创建 2 个不同的类:一个用于服务,一个用于具有接口的活动)。查看 Android 开发者指南中的绑定服务

于 2013-03-27T16:05:08.510 回答