在Android裡,用java通過實現BroadcastReceiver接口,就可以獲得Intent消息。可是Delphi程序不能直接實現JBroadcastReceiver,如何能夠實現類似Java的BroadcastReceiver功能呢?
幸運的是Delphi提供了TJFMXBroadcastReceiver類實現了BroadcastReceiver,我們便可以以此達到目的。
下面的程序片段舉例,實現如何在Delphi下獲取USB外設拔插的消息。
uses System.SysUtils, System.Classes, System.Generics.Collections, Androidapi.JNI.USB, Androidapi.JNI.Embarcadero, Androidapi.JNIBridge, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes, Androidapi.JNI.App, Androidapi.JNI.os,...; TUsbFtdiBroadcastReceiverListener = class(TJavaLocal, JFMXBroadcastReceiverListener) private [Weak] FPort: TFTDIPort; public constructor Create(const Owner: TFTDIPort); procedure onReceive(context: JContext; intent: JIntent); cdecl; end; TFTDIPort = class(TInterfacedObject, ICommPort) protected class var FManager: JD2xxManager; class var FListener : TUsbFtdiBroadcastReceiverListener; class var FReceiver: JFMXBroadcastReceiver; class var FUsbManager:JUsbManager; class var FFilter:JIntentFilter; class procedure InitClass(Port:TFtdiPort); public function Connect(Device : String): Boolean; procedure Disconnect; ...... end; { TUsbFtdiBroadcastReceiverListener } const ACTION_USB_PERMISSION='com.android.example.USB_PERMISSION'; constructor TUsbFtdiBroadcastReceiverListener.Create(const Owner: TFtdiPort); begin inherited Create; FPort:=Owner; end; procedure TUsbFtdiBroadcastReceiverListener.onReceive(context: JContext; intent: JIntent); var action:String; Accessory:JUsbAccessory; begin action:=JStringToString(intent.getAction); if action=ACTION_USB_PERMISSION then begin if (intent.getBooleanExtra(TJUsbManager.JavaClass.EXTRA_PERMISSION_GRANTED, False)) then begin Accessory:= TJUsbAccessory.Wrap(intent.getParcelableExtra(TJUsbManager.JavaClass.EXTRA_ACCESSORY)); if JStringToString(Accessory.getManufacturer)='FTDI' then begin FPort.Connect(JStringToString(Accessory.toString)); end; end; end else if (action=TJUsbManager_ACTION_USB_DEVICE_DETACHED) then begin FPort.Disconnect; end else begin end; end; class procedure TFtdiPort.InitClass(Port:TFdiPort); beginif not Assigned(FUsbManager) then begin FUsbManager := TJUsbManager.Wrap(SharedActivityContext.getSystemService(TJContext.JavaClass.USB_SERVICE)); if not Assigned(FUsbManager) then Exit; FListener := TUsbFtdiBroadcastReceiverListener.Create(Port); if not Assigned(FListener) then Exit; FReceiver := TJFMXBroadcastReceiver.JavaClass.init(FListener); if not Assigned(FReceiver) then Exit; FFilter:=TJIntentFilter.JavaClass.init(StringToJString(ACTION_USB_PERMISSION)); if not Assigned(FFilter) then Exit; FFilter.addAction(TJUsbManager.JavaClass.ACTION_USB_ACCESSORY_DETACHED); SharedActivityContext.registerReceiver(FReceiver, FFilter); end; end;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
private Button bt1 = null;
private Button bt2 = null;
private static String filter_ACTION = "android.provider.Telephony.SMS_RECEIVED";
SMSReceiver smsReceiver = null;
public String tag = "TestActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
registerReceiver();
setContentView(R.layout.main);
bt1 = (Button) findViewById(R.id.button);
bt1.setOnClickListener(new RegisterBroadReceiver());
bt2 = (Button) findViewById(R.id.button1);
bt2.setOnClickListener(new unRegisterBroadReceiver());
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(filter_ACTION);
smsReceiver = new SMSReceiver();
TestActivity.this.registerReceiver(smsReceiver, filter);
}
class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(tag, "SMSReceiver");
Toast.makeText(TestActivity.this, "新短信"+intent.getStringExtra("yaner"), Toast.LENGTH_LONG).show();
}
}
class RegisterBroadReceiver implements OnClickListener {
@Override
public void onClick(View v) {
Log.d(tag, "RegisterBroadReceiver");
Intent mIntent = new Intent(filter_ACTION);
mIntent.putE......余下全文>>
建議您到MIUI論壇咨詢一下開發人員:www.miui.com/forum.php