2010年7月21日 星期三

實作IPC LED控制程式


以下程式用於DMA-6410L 開發平台,其步驟如下:
1.新增新的activity元件。
2.修改activityMain.java,更換執行元件。
3.新增AIDL檔案。
4. 新增LED Service程式。
5. 修改LED Activity程式。
6. 修改Manifest檔案。
7. 安裝與測試













LedIPCactivity.java程式列表

package tw.com.dmatek.dma6410xp.ipc;

import tw.com.dmatek.dma6410xp.R;
import tw.com.dmatek.dma6410xp.led.Linuxc;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;

public class LedIPCactivity extends Activity {
/** Called when the activity is first created. */

private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;

public int state1=0,state2=0;
public int led_on = 1;
public int led_off = 2;
public int fd = 0;


private Button.OnClickListener button1_listener= new Button.OnClickListener()
{
public void onClick(View v)
{



//Linuxc.send(2, led_on);
try {
ledService.TurnOn(2);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
};

private Button.OnClickListener button4_listener= new Button.OnClickListener()
{
public void onClick(View v)
{


// Linuxc.send(2, led_off);
try {
ledService.TurnOff(2);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
};

private Button.OnClickListener button5_listener= new Button.OnClickListener()
{
public void onClick(View v)
{


//Linuxc.send(1, led_off);
try {
ledService.TurnOff(1);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};



private Button.OnClickListener button2_listener= new Button.OnClickListener(){
public void onClick(View v)
{

//Linuxc.send(1, led_on);
try {
ledService.TurnOn(1);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
};
private Button.OnClickListener button3_listener= new Button.OnClickListener(){
public void onClick(View v)
{

finish();
}
};
private ILedService ledService=null;
private ServiceConnection mConnection = new ServiceConnection(){

@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
ledService=ILedService.Stub.asInterface(arg1);
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub

}

};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.led);

mButton1=(Button)findViewById(R.id.led_myButton1);
mButton2 =(Button) findViewById(R.id.led_myButton2);
mButton3 =(Button) findViewById(R.id.led_myButton3);
mButton4 =(Button) findViewById(R.id.led_myButton1_1);
mButton5 =(Button) findViewById(R.id.led_myButton2_1);


bindService(new Intent(LedIPCactivity.this, LedService.class), mConnection , Context.BIND_AUTO_CREATE);


mButton1.setOnClickListener(button1_listener);

mButton2.setOnClickListener(button2_listener);


mButton3.setOnClickListener(button3_listener);
mButton4.setOnClickListener(button4_listener);
mButton5.setOnClickListener(button5_listener);

}
}

LedService.java列表

package tw.com.dmatek.dma6410xp.ipc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import tw.com.dmatek.dma6410xp.led.Linuxc;;

public class LedService extends Service {
public int led_on = 1;
public int led_off = 2;
public int fd = -1;
private ILedService.Stub mbinder=new ILedService.Stub(){

@Override
public void TurnOff(int port) throws RemoteException {
// TODO Auto-generated method stub
if(fd>=0) Linuxc.send(port, led_off);
}

@Override
public void TurnOn(int port) throws RemoteException {
// TODO Auto-generated method stub
if(fd>=0) Linuxc.send(port, led_on);
}

};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
fd=Linuxc.openled();
return mbinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Linuxc.closeled();
return super.onUnbind(intent);

}

}

沒有留言:

張貼留言