我是android平台的新手,我希望制作一个没有终端设备和协调器的应用程序,它们将通过短信进行通信。
我已经在 j2me 中构建了它,但现在在 android 中我想创建一个在后台接收短信并不间断更新数据库的服务。我已经编写了一些在简单类中运行良好但在服务中如何绑定它的代码>
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private SMSSync sync;
private Context context;
public SMSReceiver(Context ctx){
sync = new SMSSync(ctx);
this.context = ctx;
}
@Override
public void onReceive(Context context,final Intent intents){
if (intents.getAction().equals(SMS_RECEIVED)) {
new Thread(){
Context context;
Thread Set(Context ctx){
this.context=ctx;
return this;
}
public void run(){
try{
SmsManager sms = SmsManager.getDefault();
Bundle bundle = intents.getExtras();
/*String uriContent =intent.getDataString();
String[] str = uriContent.split(":");
String strPort = str[str.length-1];
short Port = Short.valueOf(strPort, 16);*/
//if(Port==5000){*/
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
String msg=null;
String address = null;
for (SmsMessage message : messages) {
msg = message.getMessageBody();
address = message.getOriginatingAddress();
}
////////////sending to SMSSync class//////////////
MainThread th = new MainThread(sync,msg);
try{
th.thread.join();
}catch(Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
if(msg.substring(3, 4).equals("2"))
ConstantClass.isAuditrequestSend = true;
// sync.smsProcess(msg);
//Toast.makeText(context, "Message Received Successfully", Toast.LENGTH_SHORT).show();
}
// }
////////////////after receiving the sms opening the Main Screen./////////////////////
Intent intent = new Intent(context,ZigbeeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}catch(Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}.Set(context).start();
}
}
//}
}