我正在尝试启动一项服务,只要导航活动正在运行,该服务就会在后台运行。问题是服务似乎没有启动。该应用程序没有崩溃,据我所知,它会尝试启动服务,但似乎什么也没发生。有人可以告诉我我做错了什么吗?此外,我正在尝试使用本地广播公司将数据字符串从服务发送到调用活动,我是否正确实现了它?最后,我似乎无法让导航活动执行 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);
}
}